{转}spring基础总结

IOC控制反转:
依赖注入:
需要的jar文件
dist\spring.jar
lib\jakarta-comnons\comnons-logging.jar
lib\aspectj\aspectjweaver.jar
aspectjrt.jar
lib/cglib/cglib nodep2.1_3.jar
lib\j2ee\common-annotation.jar
实例化Spring容器的方法
ApplicationContext ctx= new ClassPathXmlApplication(new String[]("beans.xml"));

三种实例化bean的方法
1、使用构造器实例化:(绝大部分都是用该方法)
ApplicationContext ctx= new ClassPathXmlApplication(new String[]("beans.xml"));
<bean id="orderService" class="bean的路径"></bean>
2、静态工厂方法实例化:
public class OrderFactory{
public static OrderServiceBean createOrder(){
   return new OrderServiceBean();
}
}
<bean id="orderService" class="工厂类路径" factory-method="工厂类方法名称createOrder"></bean>
3、实例工厂方法实例化
public class OrderFactory{
public OrderServiceBean createOrder(){
   return new OrderServiceBean();
}
}
<bean id="orderServiceFactory" class="工厂类路径"></bean>
<bean id="orderService" factory-bean="工厂类路径" factory-method="工厂类方法名称createOrder"></bean>

spring容器管理的bean对象的作用于
1、默认情况下是单实例(同一个bean);
2、<bean id="orderService" class="bean的路径" scope="prototye"></bean>添加了scope属性指定作用域时可以在每次getBean方法调用时生成一个新的实例;

spring管理的bean的生命周期
1、在默认情况下,在容器实例化的时候就对bean进行实例化了;
2、当scope="prototye"时,就在getBean方法时才实例化bean;
3、可以在beans.xml文件中设置lazy-init="true"来延迟初始化(在容器实例化的时候bean不实例化),实际中一般不用这个属性;
4、可以在beans.xml文件中设置init-method="方法名称"来在实例化bean完成后立刻调用该方法;
5、可以在beans.xml文件中设置destroy-method="方法名称"来在bean销毁的时候调用该方法;(默认情况下只有在应用关闭的时候才会关闭spring容器实例)正常的关闭方法是:
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");(注意是抽象类)
ctx.close();


依赖注入
在程序运行期间,由外部容器动态的将以来对象注入到组件中;
############就是让容器来实例化类(被注入的类),一定要添加属性的set方法#########################
注入方法:
1、通过属性的setter方法注入:(手动)
<bean id="personDao" class="business.dao.impl.PersonDaoBean"></bean>
<bean id="personServiceBean" class="businessbeans.impl.PersonServiceBean" init-method="open" destroy-method="destroy">
<property name="pd" ref="personDao"></property>
</bean>
使用bean的构造器注入依赖对象或基本类型
被注入的类中需要有构造函数,例如
public PersonServiceBean(PersonDao pd, String name) {
   super();
   this.pd = pd;
   this.name = name;
}
2、使用内部bean的方式,但该bean不能被其他bean使用:(不常用,手动)
<bean id="personServiceBean" class="businessbeans.impl.PersonServiceBean" init-method="open" destroy-method="destroy">
   <property name="pd">
   <bean id="personDao" class="business.dao.impl.PersonDaoBean"></bean>
</property>
</bean>
3、最优雅的依赖属性注入方法:使用Field注入(一般用于注解方式,手动)
添加lib\j2ee\commons-annotation.jar
添加context容器,并打开
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xmd
@Autowired默认按类型匹配 @Resource默认按名称匹配,无匹配名称则按类型匹配(推荐Resource方法)
@Resource(可缺省name="")根据属性的名称去xml文件中寻找对应的id或name,如果找到直接注入/如果没找到匹配属性的类型与xml中配置的类型,相同则注入;
可以不写get set方法

自动装配(不推荐使用)
不需要注解,只需要在<bean></bean>标签中添加autowire属性;必须要有set方法

不同类型数据的注入
1、类类型(如上)
2、注入一个普通属性(如String name="yanzhexian");
<bean id="personDao" class="business.dao.impl.PersonDaoBean"></bean>
<bean id="personServiceBean" class="businessbeans.impl.PersonServiceBean" init-method="open" destroy-method="destroy">
<property name="pd" ref="personDao"></property>
<property name="name" value="yanzhexian"></property>
</bean>
PS:spring会自动转换注入的普通类型到类中定义的类型
3、注入集合类型(set map list等)
set类型
<property name="sets">
   <set>
    <value>第一个</value>
    <value>第二个</value>
    <value>第三个</value>
   </set>
</property>


list类型
<property name="lists">
   <list>
    <value>一</value>
    <value>二</value>
    <value>三</value>
   </list>
</property>

property类型
<property name="properties">
   <props>
    <prop key="key1">value1</prop>
    <prop key="key2">value2</prop>
    <prop key="key3">value3</prop>
   </props>
</property>

Map类型
<property name="maps">
   <map>
    <entry key="key1" value="value1"></entry>
    <entry key="key2" value="value2"></entry>
    <entry key="key3" value="value3"></entry>
   </map>
</property>

PS:集合类型的定义与遍历
private Set<String> sets=new HashSet<String>();
private List<String> lists= new ArrayList<String>();
private Properties properties =new Properties();
private Map<String,String> maps=new HashMap<String,String>();
 

//尝试输出以来注入的集合属性sets
   System.out.println("=======Set======");
   for(String value:this.getSets()){
    System.out.println(value);
   }
//尝试输出依赖注入的list集合属性lists
   System.out.println("=======List======");
   for(String value:this.getLists()){
    System.out.println(value);
   }
//尝试输出依赖注入的Properties集合属性maps
   System.out.println("=======Properties======");
   for(String key:this.getProperties().keySet()){
    System.out.println(key+"="+this.getProperties().get(key));
   }
 
//尝试输出依赖注入的Map集合属性maps
   System.out.println("=======Map======");
   for(String key:this.getMaps().keySet()){
    System.out.println(key+"="+this.getMaps().get(key));
   }

给spring配置文件减肥(在类路径下用自动扫描的方式,注,没有添加依赖注入是不会有依赖属性注入的)
1、引入context这个命名空间和xmd文件
2、<context:component-scan base-package=""></context:component-scan>
3、标注@Service业务层 @Controller控制层 @Repository DAO层 @Component其他层
@Scope指定作用域范围 @PostConstruct指定bean初始化方法 @PreDestroy指定容器容器关闭的方法
4、使用注解时当不指定bean的名称,bean的名称就是类的简单名称(第一个字母小写)
5、可以在注释后面直接添加名称来定义bean的名称@Service("NAME");


AOP代理对象(主要是对权限管理模块应用)
代理对象--------目标对象
1、拦截所有业务方法,判断用户是否有权限,有权限就允许它执行业务方法,没有权限就不允许它执行业务方法(是否有权限根据user是否为null模拟)
2、用JDK提供的Proxy代理类来创建代理对象(目标对象必须实现接口);

如果目标对象没有实现接口
1、引入cglib.jar文件
2、用cglib来创建代理对象:
Enhancer enhancer = new Enhance();
enhancer.setSuperclass(this.targetObject.getClass);
enhancer.setCallback(this);
return enhancer.create();

PS:代理类一定要实现MethodInterceptor接口,并通过该接口来调用目标类中的业务方法;
AOP概念:前置通知,后置通知,例外通知,最终通知,环绕通知
Aspect切面 joinpoint连接点(拦截到的每一个业务方法) pointcut切入点(拦截所有的业务方法)
Advice通知() Target目标对象 Weave织入 Introduction引入


用spring进行AOP编程:
1、引入jar文件
2、配置配置文件:引入AOP的命名空间

猜你喜欢

转载自qpzm45678-163-com.iteye.com/blog/1042424