13.bean属性依赖注入的简单方式
1.构造注入:<constructor-arg></constructor-arg>标签,index指bean中方法的参数下标
2.set方法注入:<property></property>
1)简单方式:
2)p命名空间简化属性依赖注入之setter方法:
在实现p命名空间之前,要先导入p标签的约束:
web层:
service层:
14.bean属性依赖注入复杂数据类型
1数组:private String[] arr;
<bean id="user" class="pojo.User">
<property name="userName" value="李钰迪">
</property>
</bean>
<bean id="user1" class="pojo.User">
<property name="userName" value="王小曼">
</property>
</bean>
<bean id="userVo" class="pojo.UserVo">
<!-- 数组注入 -->
<property name="arr">
<array>
<value>刘小姐</value>
<value>黄小姐</value>
<value>陈小姐</value>
</array>
</property>
2list简单类型集合:private List<String> list;
<!-- list 简单类型 -->
<property name="list">
<list>
<value>马老师</value>
<value>程老师</value>
<value>孙老师</value>
</list>
</property>
3list对象类型集合:private List<User> list1;
<!-- list-pojo -->
<property name="list1">
<list>
<ref bean="user"/>
<ref bean="user1"/>
</list>
</property>
4set简单类型:private Set<String> setString;
<!-- set 可去重-->
<property name="setString">
<set>
<value>陈静仪</value>
<value>陈静仪1</value>
<value>陈静仪2</value>
</set>
</property>
5map集合形式 key value 形式:private Map<String,Object> map;
<!-- map -->
<property name="map">
<map>
<entry key="username" value="王铭">
</entry>
<entry key="username1" value="王龙">
</entry>
<entry key="username2" value="王亚峰">
</entry>
<entry key="user1" value-ref="user1" >
</entry>
</map>
</property>
6Properties key value 形式:private Properties properties;
<!-- Properties -->
<property name="properties">
<props>
<prop key="username">史学峰</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
Properties properties = vo.getProperties();
System.out.println(properties.get("username5")+","
+properties.get("password"));
15.注解扫描
@Component:
使用构造 方法实例化,使用@Component,在class上使用@Component标识这个是一个bean,用spring的IOC容器管理。
使用流程:
1、在class上使用@Component标记
2、配置组件扫描器
配置组件扫描器,spring会根据配置的扫描的包路径,去扫描包下的class,如果遇到@component,去管理这个 bean。
使用spring可以应用在三层(web层、业务层、持久层),spring提供三个对应三层bean的注解:
web层:@Controller (表示一个控制器)
业务层:@Service (表示一个业务bean)
持久层:@Repository(表示一个持久层的dao)
如果遇到无法确定属于哪一层的bean 使用@Component
以上三个注解和Component完全 等价的。
@Autowired:
基于类型注入,可以设置属性上或set方法上,不需要指定将哪个bean的id注入
@autowried和@resource的区别(重点)
都可以完成属性注入,autowried是 spring提供的,resource是javaEE规范,如果系统的bean只有一个类型,可以使用autowired,如果bean有多个类型,需要autowired和qualifier结合使用
为了系统和spring解耦合,建议使用resource,因为resource是jdk的规范
@Scope(singleton或prototype)
@Scope("prototype")//多例 多例时销毁方法不会执行
@Scope("singleton")//单例时销毁方法会执行
annotation-config和component-scan区别
1.<context:annotation-config/>
将在配置文件中添加context:annotation-config标签使@Resource、@ PostConstruct、@ PreDestroy、@Autowired注解生效
context:annotation-config只针对容器中已经存在bean。
2.<context:component-scan base-package="bgs.com.spring">
除了具有<context:annotation-config />的功能之外,还具有自动将带有@component,@service,@Repository等注解的对象注册到spring容器中的功能。如果使用了<context:component-scan>所以可以去掉<context:annotation-config>。
@PostConstruct:bean的初始化
@PreDestroy:bean的销毁
16.AOP编程三步曲
1、确定目标对象 Target
orderServiceImpl
2、编写增强 Advice
代理对象的执行逻辑,比如:开启事务、提交事务
3、定义切面 Aspect
包括切入点(切点)和增强。
定义切面的好处:一个切面或以包括多个切点和增强。
17.Aop术语
1 Joinpoint(连接点):
2 Pointcut(切入点):
3 Advice(增强/通知):
4 Target(目标对象):
5 Introduction(引介):
6 Weaving(织入):
创建代理对象的过程,将增强代码织入到目标对象上
7 Proxy(代理):
就是一个代理对象
8 Aspect(切面):
18.AOP事务(配置切面,配置增强)
19.bean在applicationContext的生命周期
<bean id="per" class="pojo.Person" init-method="setUp" destroy-method="tearDown"></bean>
定义容器的后处理:
<bean class="pojo.CustomPostPro"></bean>
bean:bean对象;name:bean的名称;
测试BeanPostProcessor后处理器:
销毁bean: