spring回顾笔记
2014-01-27 21:23
357 查看
1、IoC 工厂的获得。在配置好applicationContext.xml(放在src目录下)之后,以下代码都可以获得生产bean的工厂:
2、依赖注入 applicationContext.xml
<bean id="father" class="com.dangdang.Father">
<property name="nameofson" ref="son" />
</bean>
<bean id="son" class="com.dangdang.Son"></bean>
3、scope属性,默认是singleton单例的,prototype是多例的。通常情况下,在配置文件中,单例的bean对象是不能注入多例bean对象的。也就是说,如下的写法是不行的:
<bean id="father" class="com.test.Father" > // 默认是 scope = "singleton"的
<property name = "nameofson" ref="son" />
</bean>
<bean id="son" class="com.test.Son" scope="prototype"></bean>
如果一定要在单例对象中注入多例对象,如上例子,一种解决方法是在 单例对象Father的类中,每一次都新建一个工厂( BeanFatory factory = new ClassPathXmlApplicationContext("applicationContext.xml")),然后用这个工厂去拿son( fatory.getBeans("son"))。但是一个单例对象在每次使用时候,都新建一个工厂的话,非常消耗内存,故这种做法是不妥当的。靠谱的做法是这样,让Father类继承
BeanFactoryAware这个接口,就会需要实现一个拿factory的方法,可以避免每次都新建工厂。代码如下:
Public class Father implements BeanFactoryAware{
private BeanFactory factory = null;
@override
public void setBeanFactory(BeanFactory factory) throws BeanExceptions(){
this.factory = factory; // 这样就拿到了工厂
}
factory.getBeans("son"); // 这样就可以直接去拿多例对象,而不必每次都新建工厂
}
4、bean的属性注入 (注解注入必须JDK1.6以上)
<bean id="father" class="com.test.Father" >
<property name = "nameofson" ref="son" />
<property name = "nameofson" ><ref local/bean = "son"/></property> // 引用对象有local和bean两种,local就是引用的bean对象必须是同一个xml配置文件的,bean可以不是同一个xml配置文件的。多个配置xml文件在实例化工厂的时候,可以用通配符的做法。
<property name = "nameofson" value="somevalue" />
<property name="someofson"><value>somevalue</value></property>
</bean>
属性注入也可以注入list set map等类型
<property name="nameofson">
<list> <value></value> <value></value> <ref /> </list>
</property>
<property name="nameofson">
<set> <value></value> <value></value> <ref /> </set>
</property>
<property name="nameofson">
<map> <entry key="a1"><value>1111</value></entry> <entry key="a2"> <value>222</value> </entry> </map>
</property>
5、注意功能配对
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/> <!-- 启用注解方式 -->
<context:component-scan base-package="com.mavict" /> <!-- 启用自动扫描 不需要写bean了,自动扫描生成 -->
<aop:aspectj-autoproxy /> <!-- 启用Aspectj动态代理切面编程 容器启动过程中,有代理需要产生,就会自动扫描到并产生-->
<!-- <tx:annotation-driven transaction-manager="txManager" /> --> <!-- annotation驱动的事务管理 事务管理者是txManager 整合hibernate,
就要去找hibernateTransactionManager-->
6、@Repository 一般加在类似 UserManager 类上 @Service一般加在类似 UserService类上 @Component一般加载User上,数据库持久的。但是其作用其实都是一样的。
7、aspectj 动态代理加切面类 需要包 aopaliance.jar (高版本貌似已经包含了) aspectjrt aspectjweaver。具体如下:
@Aspectj
public class DynamicProxyFactory{
@Before( executioin( * com.dang.test.*.MeInt.*(..) ) ) // 第一个星表示任何返回值;第二个表示test下的任何子类;第三个表示Melnt下的任何方法;两个点表示方法中任何参数
public void before(){ //.................. }
@After( executioin( * com.dang.test.*.MeInt.*(..) ) )
public void after(){ //.................. }
}
8、其他几个切面注解
@Around 注解的方法,需要有( ProceedingJoinPoint joinpoint )参数,然后函数体中 joinpoint.proceed(); 函数放行。在此语句前后定义工作,会在欲拦截的方法的前后执行此处定义的工作
@AfterReturning 当拦截的方法返回时执行
@AfterThrowing 当拦截的方法返回异常时执行
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
2、依赖注入 applicationContext.xml
<bean id="father" class="com.dangdang.Father">
<property name="nameofson" ref="son" />
</bean>
<bean id="son" class="com.dangdang.Son"></bean>
3、scope属性,默认是singleton单例的,prototype是多例的。通常情况下,在配置文件中,单例的bean对象是不能注入多例bean对象的。也就是说,如下的写法是不行的:
<bean id="father" class="com.test.Father" > // 默认是 scope = "singleton"的
<property name = "nameofson" ref="son" />
</bean>
<bean id="son" class="com.test.Son" scope="prototype"></bean>
如果一定要在单例对象中注入多例对象,如上例子,一种解决方法是在 单例对象Father的类中,每一次都新建一个工厂( BeanFatory factory = new ClassPathXmlApplicationContext("applicationContext.xml")),然后用这个工厂去拿son( fatory.getBeans("son"))。但是一个单例对象在每次使用时候,都新建一个工厂的话,非常消耗内存,故这种做法是不妥当的。靠谱的做法是这样,让Father类继承
BeanFactoryAware这个接口,就会需要实现一个拿factory的方法,可以避免每次都新建工厂。代码如下:
Public class Father implements BeanFactoryAware{
private BeanFactory factory = null;
@override
public void setBeanFactory(BeanFactory factory) throws BeanExceptions(){
this.factory = factory; // 这样就拿到了工厂
}
factory.getBeans("son"); // 这样就可以直接去拿多例对象,而不必每次都新建工厂
}
4、bean的属性注入 (注解注入必须JDK1.6以上)
<bean id="father" class="com.test.Father" >
<property name = "nameofson" ref="son" />
<property name = "nameofson" ><ref local/bean = "son"/></property> // 引用对象有local和bean两种,local就是引用的bean对象必须是同一个xml配置文件的,bean可以不是同一个xml配置文件的。多个配置xml文件在实例化工厂的时候,可以用通配符的做法。
<property name = "nameofson" value="somevalue" />
<property name="someofson"><value>somevalue</value></property>
</bean>
属性注入也可以注入list set map等类型
<property name="nameofson">
<list> <value></value> <value></value> <ref /> </list>
</property>
<property name="nameofson">
<set> <value></value> <value></value> <ref /> </set>
</property>
<property name="nameofson">
<map> <entry key="a1"><value>1111</value></entry> <entry key="a2"> <value>222</value> </entry> </map>
</property>
5、注意功能配对
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/> <!-- 启用注解方式 -->
<context:component-scan base-package="com.mavict" /> <!-- 启用自动扫描 不需要写bean了,自动扫描生成 -->
<aop:aspectj-autoproxy /> <!-- 启用Aspectj动态代理切面编程 容器启动过程中,有代理需要产生,就会自动扫描到并产生-->
<!-- <tx:annotation-driven transaction-manager="txManager" /> --> <!-- annotation驱动的事务管理 事务管理者是txManager 整合hibernate,
就要去找hibernateTransactionManager-->
6、@Repository 一般加在类似 UserManager 类上 @Service一般加在类似 UserService类上 @Component一般加载User上,数据库持久的。但是其作用其实都是一样的。
7、aspectj 动态代理加切面类 需要包 aopaliance.jar (高版本貌似已经包含了) aspectjrt aspectjweaver。具体如下:
@Aspectj
public class DynamicProxyFactory{
@Before( executioin( * com.dang.test.*.MeInt.*(..) ) ) // 第一个星表示任何返回值;第二个表示test下的任何子类;第三个表示Melnt下的任何方法;两个点表示方法中任何参数
public void before(){ //.................. }
@After( executioin( * com.dang.test.*.MeInt.*(..) ) )
public void after(){ //.................. }
}
8、其他几个切面注解
@Around 注解的方法,需要有( ProceedingJoinPoint joinpoint )参数,然后函数体中 joinpoint.proceed(); 函数放行。在此语句前后定义工作,会在欲拦截的方法的前后执行此处定义的工作
@AfterReturning 当拦截的方法返回时执行
@AfterThrowing 当拦截的方法返回异常时执行
相关文章推荐
- 【笔记】 Spring Boot [ 2 ] 之回顾 Spring MVC 的 WebMvcConfigurerAdapter 类
- SpringBoot 学习笔记(一)——Spring回顾与SpringMVC基础
- 【java框架】0209笔记——Spring
- 随记录 之 Spring 自学笔记(一) -- IOC(一)
- 搭建spring工程(笔记)
- spring+spring mvc + mybatis + maven 项目框架搭建笔记<一>
- Spring 笔记
- JavaWeb学习笔记-spring-12-AOP-创建增强类(异常抛出增强)
- Spring学习笔记1
- Spring学习笔记--构造器注入
- JavaWeb学习笔记-spring-14-AOP-创建切面(静态正则表达式方法匹配切面)
- spring 学习笔记
- Spring笔记(七)
- Spring4学习笔记-SpringJDBC基本操作(补充)
- SpringBoot使用Shiro验证登录笔记
- BBS项目笔记之一:Spring+Hibernate实现Dao层
- spring学习笔记(26)spring整合Quartz2持久化稳健任务调度
- 别人的spring学习入门笔记
- spring学习笔记(4)——配置文件配置bean自动装配
- Spring 学习笔记