1、Spring内部流程
继承图
ApplicationContext:在加载配置文件时,就把配置文件中所有的bean都进行实例化,然后调用getBean()时直接获取
BeanFactory:加载配置文件时,实例化延迟,在调用getBean()的时候才进行实例化。
@Test
public void testHelloSpring() {
String xmlPath = "com/exmaple/spring/beans.xml";
//获取Srping容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
xmlPath);
//通过容器获取配置文件中id,获取实例
User user = (User) applicationContext.getBean("userId");
//调用实例中的方法
user.hello();
}
2、Bean的实例化方式
1、使用默认构造方法
这种方式,比较常用,也就是只在bean.xml文件中配置就行了
<bean id="userId" class="com.exmaple.spring.User"></bean>
2、使用静态工厂
通过创建静态类,在类中创建实例,在bean.xml文件中配置工厂类
1、User类
public class User {
public void hello(){
System.out.println("hello my spring by static factory");
}
}
2、MyBeanFactory代码
public class MyBeanFactory {
//静态工厂,创建实例
public static User createUser() {
return new User();
}
}
3、beans.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.xsd">
<!-- 配置静态工厂 -->
<bean id="user" class="com.exmaple.spring3.MyBeanFactory"
factory-method="createUser"></bean>
</beans>
4、测试
@Test
public void testHelloSpring() {
String xmlPath = "com/exmaple/spring3/beans.xml";
//获取Srping容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
xmlPath);
//通过容器获取配置文件中id,获取实例
User user = (User) applicationContext.getBean("user");
//调用实例中的方法
user.hello();
}
3、使用普通工厂
使用这种模式,首先得实例化工厂
1、User类
public class User {
public void hello(){
System.out.println("hello my spring by factory");
}
}
2、MyBeanFactory类中代码
public class MyBeanFactory {
//使用普通方式创建
public User createUser() {
return new User();
}
}
3、bean.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.xsd">
<!-- 工厂 -->
<bean id="myBeanFactory" class="com.exmaple.spring4.MyBeanFactory"></bean>
<bean id="user1" factory-bean="myBeanFactory" factory-method="createUser"></bean>
</beans>
4、测试
@Test
public void testHelloSpring() {
String xmlPath = "com/exmaple/spring4/beans.xml";
//获取Srping容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
xmlPath);
//通过容器获取配置文件中id,获取实例
User user = (User) applicationContext.getBean("user1");
//调用实例中的方法
user.hello();
}
3、Bean的种类
普通bean:
工厂bean:特殊的bean,用于生成指定的bean,必须实现接口FactoryBean,这个接口用于返回指定的bean实例
BeanFactory:这是一个工厂,用于生成bean,生成任意的bean.
FactoryBean:这是一个bean,用于生成指定的bean.
4、id和name属性
<bean id="userId" class="com.exmaple.spring.User"></bean>
id:不能重复,可以使用特殊字符和数字
name:可以设置多个name,但是不能重复
5、Bean的作用域
6、Bean的生命周期
7、Bean的注入
1、xml注入:
- 构造函数注入:构造注入的优势是通过构造强制依赖关系
- 属性setter方法注入: set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选
- 接口注入 – spring不支持
2、自动注入
- byType:按类型装配
- byName:按名称装配
- constructor装配
- autodetect 不确定装配
构造注入
public class User {
private String name ;
private int age ;
private String country ;
public User(String name, int age, String country) {
this.name = name;
this.age = age;
this.country = country;
}
public String toString(){
return name + " is " + age + " years old,living in " + country ;
}
}
bean.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans">
<bean id="user" class="com.zcl.spring.setterinjection.User">
<constructor-arg value="Zhao" />
<constructor-arg value="22" />
<constructor-arg value="China" />
</bean>
</beans>
8、SPEL
Spring3.0引入全新的SpEL(Spring Expression Language)Spring表达式语言,使用表达式语言可以使属性注入格式进行进一步简化,同时支持包括对象注入、集合注入、集合访问、实例方法引用、静态方法引用等多种格式
格式:<bean value="#{表达式}">
语法:
9、配置Bean基于注解
注解:就是一个类,用于取代xml文件,使配置更加简化
xml配置:容易查找,但内容多,不易维护
注解配置:将注解添加在接口或者类上,不直观,不易查找。
注解:
@Component 组件,用于取代配置,建议配置任意内容。
@Controller ,用于配置web层,之后使用在action上。
@Service,用于配置service层。
@Repository ,用于配置dao层。
@AutoWired 自动注入,默认情况安装类型进行注入
@Qualifier 修改自动注入,从默认按照类型匹配,变成按照名称匹配。
@Resource 与 @AutoWired和@Qualifier结合 等效
例如:@Resource(“名称”)
在MVC项目中的对应
Controller层:action中配置 @Controller,并使用@AutoWired进行自动注入
Service层:配置@Service,且使用@AutoWired和@Qualifier 确定使用指定的dao
Dao层:dao中配置@Repository
10、整合Junit
导入spring测试jar – spring-test-3.2.0.RELEASE.jar
两个注解
@RunWith 用于确定使用按个第三方测试功能
例如:Junit4 实现类 SpringJunit4ClassRunner
@ContextConfiguration 用于配置xml文件
locations 确定xml位置。