Spring的注解装配Bean
Spring2.5 引入使用注解去定义Bean
@Component 描述Spring框架中Bean
Spring的框架中提供了与@Component注解等效的三个注解
@Repository 用于对DAO实现类进行标注
@Service 用于对Service实现类进行标注
@Controller 用于对Controller实现类进行标注
***** 三个注解为了后续版本进行增强的
使用注解,那么applicationContext.xml配置文件要作出修改
<?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"> <context:annotation-config/> <!--支持注解--> <context:component-scan base-package="cn.yzu.spring3"/> <!--自动扫描--> </beans>
Bean的属性注入
普通属性
@Value(value="你好") private String info;
对象属性
@Autowired:自动装配默认使用类型注入.
@Qualifier("userDao"):按名称进行注入.
@Autowired @Qualifier("userDao") private UserDao userDao;
等价于
@Resource(name="userDao") private UserDao userDao;
Bean其他的属性的配置
配置Bean初始化方法和销毁方法:init-method 和 destroy-method
@PostConstruct 初始化
@PreDestroy 销毁
配置Bean的作用范围:@Scope("singleton")
@Service(value="userService") @Scope("singleton") public class UserService { @Value(value="你好") private String info;
@Resource(name="userDao") private UserDao userDao;
public void sayHello(){ System.out.println("Hello Spring Annotation..."+info); } @PostConstruct public void setup(){ System.out.println("初始化..."); } @PreDestroy public void teardown(){ System.out.println("销毁..."); } }
Spring3.0提供使用Java类定义Bean信息的方法(用的很少)
@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; } }
@Test public void demo1(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Car car = (Car) applicationContext.getBean("car"); Product product = (Product) applicationContext.getBean("product"); System.out.println(car); System.out.println(product); }
实际开发中使用XML还是注解?
XML:有利于bean管理
注解:注入属性的时候比较方便
两种方式结合:一般使用XML注册Bean,使用注解进行属性的注入
<?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"> <!--使@Resource、@ PostConstruct、@ PreDestroy、@Autowired注解生效--> <context:annotation-config/> <bean id="orderDao" class="cn.yzu.spring3.demo3.OrderDao"></bean> <bean id="customerService" class="cn.yzu.spring3.demo3.CustomerService"></bean> </beans>
public class CustomerService { @Autowired @Qualifier("orderDao") private OrderDao orderDao; @Override public String toString() { return "CustomerService [customerDao=" + customerDao + ", orderDao="+ orderDao + "]"; } }