SSM框架之Spring IOC学习笔记:
Spring需要的配置文件:applicationContext.xml常用的文件头
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
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">
</beans>
依赖注入(DI):
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
private String name; // 1.普通值注入
private String[] books; //3.数组注入
private Wife wife; //2.Bean注入
private List<String> hobbys; //4.List注入
private Map<String,String> card; //5.Map注入
private Set<String> game; //6.Set注入
private String Address; //7.null注入
private Properties info; //
// 以上属性的get、set方法...
}
1.普通值注入:
<bean id="student" class="com.haihai.substance.Student"> <!-- class中为全包名,id为唯一标识 -->
<property name="name" value="haihai"></property> <!-- name对应实体类中的属性,value为要赋的值 -->
</bean>
2.Bean注入:
<bean id="wife" class="com.haihai.substance.Wife"></bean>
<bean id="student" class="com.haihai.substance.Student">
<property name="wife" ref="wife"></property>
</bean>
3.数组注入
<bean id="student" class="com.haihai.substance.Student">
<property name="books">
<array>
<value>狼王梦</value>
<value>羊皮卷</value>
<value>墨菲定律</value>
</array>
</property>
</bean>
4.List注入
<bean id="student" class="com.haihai.substance.Student">
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
<value>打游戏</value>
</list>
</property>
</bean>
5.Map注入
<bean id="student" class="com.haihai.substance.Student">
<property name="card">
<map>
<entry key="身份证" value="..."></entry>
<entry key="银行卡" value="..."></entry>
</map>
</property>
</bean>
6.Set注入
<bean id="student" class="com.haihai.substance.Student">
<property name="games">
<set>
<value>LoL</value>
<value>BOB</value>
</set>
</property>
</bean>
7.null注入
<bean id="student" class="com.haihai.substance.Student">
<property name="address">
<null />
</property>
</bean>
8.Properties注入
<bean id="student" class="com.haihai.substance.Student">
<property name="info">
<props>
<prop key="driver">value_one</prop>
<prop key="url">localhost</prop>
<prop key="username">root</prop>
<prop key="password">123123</prop>
</props>
</property>
</bean>
9.p命名空间注入
<bean id="wife" class="com.haihai.substance.Wife"></bean>
<bean id="student" class="com.haihai.substance.Student"
p:name="Lhh"
p:address="BeiJing"
p:wife-ref="wife"> //bean注入方式: p:{属性名}-ref="bean的id"
</bean>
读取applicationContext.xml文件并创建Bean容器对象:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)context.getBean("student");
Bean的作用域
1.singleton单例模式(默认)
<bean id="student" class="com.haihai.substance.Student" scope="singleton"></bean> //singleton表示单例模式,只会创建一个bean对象
2.prototype原型模式(多例模式)
<bean id="student" class="com.haihai.substance.Student" scope="prototype"></bean> //每次从容器中取时都会产生一个新对象
3.request、session、application 只用于web开发中
Bean的自动装配
public class Person {
private Dog dog;
private Cat cat;
private String name;
// 以上属性的get、set方法...
}
<bean id="cat" class="com.haihai.substance.Cat"></bean>
<bean id="dog" class="com.haihai.substance.Dog"></bean>
<bean id="person" class="comm.haihai.substance.Person" autowire="byType"
p:name="Lhh">
</bean> //自动装配byType根据属性类型自动装配,但每个属性类型只能有一个Bean
<bean id="cat" class="com.haihai.substance.Cat"></bean>
<bean id="dog" class="com.haihai.substance.Dog"></bean>
<bean id="person" class="comm.haihai.substance.Person" autowire="byName"
p:name="Lhh">
</bean>
//自动装配byName根据属性名自动装配,但bean中的id要和属性名一致
//例如:id为cat 属性名为cat 可以
// id为catone 属性名为cat 不可以
注解方式自动装配
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
//开启注解支持,否则无法使用注解
<context:annotation-config />
//表示扫描哪个位置的包,使该包下的注解生效,使用这个后可以不使用<context:annotation-config />
<context:component-scan base-package="com.haihai" />
<bean id="cat" class="com.haihai.substance.Cat"></bean>
<bean id="dog" class="com.haihai.substance.Dog"></bean>
<bean id="person" class="com.haihai.substance.Person"></bean>
</beans>
public class Person {
@Autowired //该注解的自动注入与类型相关,找到多个在与属性名匹配
private Dog dog;
@Autowired
private Cat cat;
private String name;
// 以上属性的get、set方法...
}
自动装填时设置为可以null
public class Person {
@Autowired(required = false) //这时表示dog属性可以为null
private Dog dog;
@Autowired
private Cat cat;
private String name;
// 以上属性的get、set方法...
}
public class Person {
private Dog dog;
private Cat cat;
private String name;
public Dog getDog() {
return dog;
}
public void setDog(@Nullable Dog dog) { //表示dag可以为null
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Autowired 与 @Qualifier(value=“xxx”) 当bean配置中相同类型较多时,使用这两个注解来指定唯一的一个Bean对象!!
public class Person {
@Autowired
@Qualifier(value = "dog")
private Dog dog;
@Autowired
private Cat cat;
private String name;
// 以上属性的get、set方法...
}
@Resource 是先根据属性名查找在根据属性类型查找
Spring中的注解开发:
1.@Autowired
2.@Resource
3.@Nullable
这三个注解看上文
4.@Component : 放在类上,表示该类已经被Spring管理,也就不需要使用xml配置bean了
使用到MVC三层架构中的注解,这四个注解作用相同,名字用来区分所在层次
*dao : @Repository
*service : @Service
*controller : @Controller
5.@Value(“”) : 用来赋值
//该注解相当于 <bean id="person" class="com.haihai.substance.Person"></bean>
@Component
public class Person {
private Dog dog;
private Cat cat;
//该注解相当于 <property name="username" value="Lhh">
@Value("Lhh")
private String username;
// 以上属性的get、set方法...
}
6.@Scope(“singleton”) 表示单例模式,放在类上