Skip to content

Instantly share code, notes, and snippets.

@kmb385
Created January 15, 2014 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmb385/8429322 to your computer and use it in GitHub Desktop.
Save kmb385/8429322 to your computer and use it in GitHub Desktop.
Simple Spring Injection with @resource
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:annotation-config/>
<bean name="myBean" class="com.test.MyBeanImpl"/>
<bean name="myApp" class="com.test.MyApp"/>
</beans>
package com.test;
import javax.annotation.Resource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyApp {
@Resource(name="myBean")
MyBeanImpl myBean;
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/context.xml");
MyApp app = context.getBean(MyApp.class);
System.out.println(app.myBean.getName());
}
}
package com.test;
public class MyBeanImpl implements MyService, MyInterface1, MyInterface2 {
private String name = "Kevin";
@Override
public String getName() {
return name;
}
}
package com.test;
public interface MyInterface1 {
}
package com.test;
public interface MyInterface2 {
}
package com.test;
public interface MyService {
public String getName();
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringExample</groupId>
<artifactId>SpringExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment