Java Spring-注解进行属性注入
2017-11-06 21:19:43
一、Spring的注解装配Bean
Spring2.5 引入使用注解去定义Bean
- @Component 描述Spring框架中Bean
Spring的框架中提供了与@Component注解等效的三个注解
- @Repository 用于对DAO实现类进行标注(dao层)
- @Service 用于对Service实现类进行标注(service层)
- @Controller 用于对Controller实现类进行标注(web层)
//因为只有一个属性value,所以可以直接写。一般需要value="..." @Component("user") public class User { public void sayHello(){ System.out.println("Hello World."); } }
配置文件:
<?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.xsd"> <!-- bean definitions here --> <context:component-scan base-package="spring5"/> </beans>
二、注解进行属性注入
普通属性:@Value(value="..."),这时候可以不写setter方法
对象属性:@Resource(name = "....")
或者采用 @Autowired
@Qualifier(value = "plane")
//因为只有一个属性value,所以可以直接写。一般需要value="..." @Component("user") public class User { @Value(value="Spring") private String s; public void sayHello(){ System.out.println("Hello World."); } @Override public String toString() { return "User{" + "s='" + s + '\'' + '}'; } }
三、XML和注解的混合使用
两种方式结合:一般使用XML注册Bean,使用注解进行属性的注入
首先介绍一些其他的注解配置:
(1)配置 Bean 初始化方法和销毁方法 :
* init-method 和 destroy-method.
- @PostConstruct 初始化
- @PreDestroy 销毁
@PostConstruct public void setup(){ System.out.println("初始化..."); } @PreDestroy public void teardown(){ System.out.println("销毁..."); }
(2) 配置 Bean 的作用范围 :@Scope
@Component("user") @Scope(value="prototype") public class User { @Value(value="Spring") private String s; public void sayHello(){ System.out.println("Hello World."); } @Override public String toString() { return "User{" + "s='" + s + '\'' + '}'; } }
(3)使用Java类来进行配置信息,在XML中扫描一下即可
@Configuration public class BeanConfig { @Bean(name = "car") public Car showCar() { Car car = new Car(); car.setName("长安"); car.setPrice(40000d); return car; } @Bean(name = "product") public Product initProduct() { Product product = new Product(); product.setName("空调"); product.setPrice(3000d); return product; } }
混合使用范例:
public class Person { @Autowired @Qualifier("car") private Car car; @Autowired @Qualifier(value = "plane") private Plane plane; @Override public String toString() { return "Person{" + "car=" + car + ", plane=" + plane + '}'; } }
配置文件:
<?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.xsd"> <!-- bean definitions here --> <!--使属性注入注解生效--> <context:annotation-config/> <bean id="car" class="spring6.Car"/> <bean id="plane" class="spring6.Plane"/> <bean id="person" class="spring6.Person"> </bean> </beans>
四、集成Junit测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-config.xml") public class spring6 { @Autowired @Qualifier("person") private Person p; @Test public void demo(){ System.out.println(p); } }
配置文件:
<?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.xsd"> <!-- bean definitions here --> <!--使属性注入注解生效--> <context:annotation-config/> <bean id="car" class="spring6.Car"/> <bean id="plane" class="spring6.Plane"/> <bean id="person" class="spring6.Person"> </bean> </beans>
如果是注解进行的类注册,那么需要使用<context:component-scan base-package="spring5"/>来扫包,
不过一般bean的注册还是在XML文件中声明的,所以在注解方式中用的比较多的就是属性的注入操作了,这种操作只需要加上<context:annotation-config/>,就可以使属性注入的注解生效。