Spring笔记(未完)
★、要使用的jar
dist/spring.jarlib/jakarta-commons/commons-logging.jar
如果使用aop,还需要
lib/aspectj/aspectjweaver.jarlib/aspectj/aspectjrt.jarlib/cglib/cglib-nodep-2.1_3.jar
如果使用了jsr-250中的注解,还需要
lib/j2ee/common-annotabions.jar
★、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="" name="code">ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
方法二:在文件系统路径下寻找配置文件来实例化容器(不推荐使用,linux下不好使)
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"d:/applicationContext.xml"});
★、实例化bean的三种方式
1. 用构造器来实例化
<bean id="exampleBean" name="code"><bean id="exampleBean" name="code"><!-- the factory bean, which contains a method called createInstance() --><bean id="serviceLocator" name="code"><bean id="accountService" scope="prototype"/>
1. singleton在spring容器实例化的时候实例化,prototype在getBean的时候才初始化(建议使用,有问题的话可以在启动的时候就看出来)
2. 可以给bean加一个lazy-init="true"来让singleton不要在spring容器初始化的时候实例化(不建议使用)
<bean id="person" lazy-init="true"></bean>
3. 如果想为所有bean进行延迟初始化,可以给beans加一个属性default-lazy-init=”true” (不建议使用)
4. 可以给bean加一个属性init-method=”init”和destroy-method=”destroy”在实例化该bean之后调用init方法,spring容器关闭后,会自动调用destroy方法
<bean id="person" init-method="init" destroy-method=”destroy”></bean>
public Person(){//构造函数}public void init(){//调用完构造函数会来调用这个方法,方法名是可以自定义的,可以在这里做一些事情,比如对资源进行打开,等}public void destroy(){}
ApplicationContext ctx = ……ctx.close();
★、依赖注入
1. 构造器注入
无参
package x.y;public class Foo { public Foo(Bar bar, Baz baz) { // ... }}
<beans> <bean name="foo" name="code">// 有参构造函数// 当使用简单类型时,比如<value>true<value>,Spring将无法知道该值的类型。不借助其他帮助,他将无法仅仅根据参数类型进行匹配package examples;public class ExampleBean { // No. of years to the calculate the Ultimate Answer private int years; // The Answer to Life, the Universe, and Everything private String ultimateAnswer; public ExampleBean(int years, String ultimateAnswer) { this.years = years; this.ultimateAnswer = ultimateAnswer; }}
// 可以通过使用'type'属性来显式指定那些简单类型的构造参数的类型,比如:
<bean id="exampleBean" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/></bean>
// 还可以通过index属性来显式指定构造参数的索引,比如下面的例子:
<bean id="exampleBean" value="7500000"/> <constructor-arg index="1" value="42"/></bean>
2. setter注入
public class ExampleBean { private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; public void setBeanOne(AnotherBean beanOne) { this.beanOne = beanOne; } public void setBeanTwo(YetAnotherBean beanTwo) { this.beanTwo = beanTwo; } public void setIntegerProperty(int i) { this.i = i; } }
<bean id="exampleBean" ref="yetAnotherBean"/> <property name="integerProperty" value="1"/></bean><bean id="anotherExampleBean" class="examples.YetAnotherBean"/>
3. Field注入(用于注解方式),用@Resource注解完成属性装配
可以减少applicationContext.xml的代码
剩下的有时间再发
★、Spring自动扫描和管理bean
★、基于注解使用AOP
★、基于XML使用AOP
★、Spring和JDBC组合开发和事物管理