<?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" xmlns:p="http://www.springframework.org/schema/p" 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:component-scan base-package="com.sxh.spring.annotaion"/> </beans>
2.简单的注解例子
@Component("customerService") //相当于<bean id="customerService"...> public class CustomerService { @Value("lisi") //相当于<property name="name" value="lisi"> private String name; public void list(){ System.out.println("name:"+name); System.out.println("service list()..."); } }
3.框架常用注解(和上面@Component功能相同,区分层次)
@Controller--Action @Service--Service @Repository--Dao
4.复杂属性注入
//方式1:spring2.5规范 @Autowired @Qualifier("customerDao1") private CustomerDao customerDao1; //方式2:JSR-250,需要配置<context:annotation-config/> //<!-- 使@Resource,@PostConstruct,@PreDestory,@Autowired 生效 --> @Resource(name="customerDao2") private CustomerDao customerDao2; //方式3:JSR-443,需要javax.inject-1.jar @Inject @Named("customerDao3") private CustomerDao customerDao3;
[color=darkred][/color]