[size=x-large][color=darkred]1.xml配置开启注解[/color][/size]
[size=x-large][color=darkred]2.简单的注解例子[/color][/size]
[size=x-large][color=darkred]3.框架常用注解(和上面@Component功能相同,区分层次)[/color][/size]
[size=x-large][color=darkred]4.复杂属性注入[/color][/size]
[size=x-large][color=darkred][/color][/size]
<?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>
[size=x-large][color=darkred]2.简单的注解例子[/color][/size]
@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()...");
}
}
[size=x-large][color=darkred]3.框架常用注解(和上面@Component功能相同,区分层次)[/color][/size]
@Controller--Action
@Service--Service
@Repository--Dao
[size=x-large][color=darkred]4.复杂属性注入[/color][/size]
//方式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;
[size=x-large][color=darkred][/color][/size]