applicationContext配置模板
<?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">
<!--导入其他spring配置-->
<import resource="{path}/beans.xml"/>
<!--设置别名:在获取Bean的时候可以使用别名获取-->
<alias name="hello" alias="hello123"/>
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
</beans>
1.IOC方面
jar包和测试
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
@Test
public void test(){
//解析beans.xml文件 , 生成管理相应的Bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//getBean : 参数即为spring配置文件中bean的id .
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
1.注入方式:
(1)构造器无参注入(略)
构造器有参注入
<!-- 有参注入一共有三种方式 -->
<bean id="user" class="com.jyy.pojo.User">
<!-- index指构造方法 , 下标从0开始 -->
<constructor-arg index="0" value="jyy"/>
<!-- name指参数名 -->
<constructor-arg name="name" value="jyy"/>
<!-- type指类型 -->
<constructor-arg type="java.lang.String" value="jyy"/>
</bean>
(2)Set注入(需要该属性写了setter方法)(如果属性是boolean类型 , 没有set方法 , 是 is)
<!--value注入基本类型,ref注入引用-->
<bean id="student" class="com.jyy.pojo.Student">
<property name="name" value="张三"/>
<property name="teacher" ref="teacher1"/>
</bean>
<bean id="teacher1" class="com.jyy.pojo.Teacher">
<property name="name" value="老师"/>
</bean>
(3)数组Array / List / Map / Set / Null空值 / Properties类
<bean id="student" class="com.jyy.pojo.Student">
<!--1.普通值注入-->
<property name="name" value="张三" />
<!--2.引用注入-->
<property name="address" ref="address" />
<!--3.array注入-->
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!--4.list注入-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property>
<!--5.map注入-->
<property name="card">
<map>
<entry key="身份证" value="12312414"></entry>
<entry key="银行卡" value="3121312"></entry>
</map>
</property>
<!--6.set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--6.空值注入 eg. String wife = null-->
<property name="wife">
<null/>
</property>
<!--7.Properties类注入-->
<property name="info">
<props>
<prop key="学号">123124</prop>
<prop key="性别">man</prop>
</props>
</property>
</bean>
(4)p命名(代替property标签进行setter注入) 和 c命名(代替constructor-arg标签进行构造器注入):
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.jyy.pojo.User" p:name="张三" p:age="22"/>
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法,同时需要有参构造器-->
<bean id="user" class="com.jyy.pojo.User" c:name="张三" c:age="23"/>
(5)autowire标签实现自动装配(没啥大用处,不如注解方便)
假如user类中有cat和dog属性,可以通过autowire自动帮我们配置
<bean id="dog" class="com.jyy.pojo.Dog"/>
<bean id="cat" class="com.jyy.pojo.Cat"/>
<!--通过类型自动注入-->
<bean id="user" class="com.kuang.pojo.User" autowire="byType">
<property name="str" value="主人"/>
</bean>
<!--通过名称自动注入-->
<bean id="user" class="com.kuang.pojo.User" autowire="byName">
<property name="str" value="主人"/>
</bean>
2. Bean作用域/生命周期,scope属性
使用标签:scope
<bean id="user" class="com.jyy.pojo.User" scope="singleton">
3.注解
步骤:aop包 + xml中开启注解 + 类中使用注解
使用注解需要导入aop的包
在spring配置文件中引入context文件头,并开启注解
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
<context:annotation-config/>
3.1 对类中的引用类型属性注解(代替<property ref>标签)
(1)@Autowired(需要aop的包,自动根据类型匹配)
@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null
User类
public class User {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String str;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getStr() {
return str;
}
}
配置文件
<context:annotation-config/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>
(2)@Qualifier:不能单独使用
@Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配
<bean id="dog1" class="com.kuang.pojo.Dog"/>
<bean id="dog2" class="com.kuang.pojo.Dog"/>
<bean id="cat1" class="com.kuang.pojo.Cat"/>
<bean id="cat2" class="com.kuang.pojo.Cat"/>
@Autowired
@Qualifier(value = "cat2")
private Cat cat;
@Autowired
@Qualifier(value = "dog2")
private Dog dog;
如果不加@Qualifer,系统无法分辨,因为bean中有两个Cat类和Dog类
(3)@Resource
@Resource如有指定的name属性,先按该属性进行byName方式查找装配;其次再进行默认的byName方式进行装配;如果以上都不成功,则按byType的方式自动装配。
public class User {
//如果允许对象为null,设置required = false,默认为true
@Resource(name = "cat2")
private Cat cat;
@Resource
private Dog dog;
private String str;
}
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat1" class="com.kuang.pojo.Cat"/>
<bean id="cat2" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>
总结:@Autowired, @Qualifier, @Resource这三个注解写在类的属性上(引用类),注入bean时无需再用<property>标签
3.2 对类进行注解(代替<bean>标签)
配置扫描哪些包下的注解
<context:component-scan base-package="com.jyy.pojo"/>
@Component 对类进行配置
@Component("user")
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
public String name = "张三";
}
@Component等价于以下三种,
@Controller:web层
@Service:service层
@Repository:dao层
3.3 对类中的基本类型注解(代替<property value>标签)
(1)如果不用set方法,直接在属性上加注解
@Component("user")
public class User {
@Value("张三")
public String name;
}
(2)如果提供了set方法,在set方法上加注解
@Component("user")
public class User {
public String name;
@Value("张三")
public void setName(String name) {
this.name = name;
}
}
3.4 声明周期
直接在类上加@Scope注解
@Controller("user")
@Scope("prototype")
public class User {
@Value("张三")
public String name;
}
3.5 < context:annotation-config / > 和 < context:component-scan / >区别
<context:annotation-config>:给类中属性赋值(@Autowired) ,但仍然需要在xml中配置bean,把类手动加到容器中。
< context:component-scan / >:<context:annotation-config> + 自动把扫描出的类加入容器(@Component)
Reference:
狂神说公众号:https://www.bilibili.com/video/BV1WE411d7Dv
概述及IOC理论推导: https://mp.weixin.qq.com/s/VM6INdNB_hNfXCMq3UZgTQ
快速上手Spring: https://mp.weixin.qq.com/s/Sa39ulmHpNFJ9u48rwCG7A
依赖注入(DI): https://mp.weixin.qq.com/s/Nf-cYENenoZpXqDjv574ig
自动装配: https://mp.weixin.qq.com/s/kvp_3Uva1J2Q5ZVqCUzEsA
使用注解开发: https://mp.weixin.qq.com/s/dCeQwaQ-A97FiUxs7INlHw