Spring day1
1.Spring
简介
- Spring:春天----->给软件行业带来了春天
- Spring是一个轻量级控制反转(ioC)和面向切面(AOP)的容器框架
3.HelloSpring
4.IOC创建对象的方式
-
使用无参构造方法,默认!
<bean id="user" class="com.sheng.pojo.User"> <property name="name" value="123"></property> </bean> -
假设我们要使用有参构造创建对象。
-
下标值
<bean id="user" class="com.sheng.pojo.User">--> <constructor-arg index="0" value="123"> </constructor-arg> -
类型
<bean id="user" class="com.sheng.pojo.User"> <constructor-arg type="com.sheng.pojo.User" value="123"> </constructor-arg> </bean> -
参数名
<!--第三种有参构造:直接通过参数名来赋值--> <bean id="user" class="com.sheng.pojo.User"> <constructor-arg name="name" value="123"> </constructor-arg> </bean>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了。
-
5、Spring配置
5.1 别名
<!--别名,如果添加了别名,我们也可以使用别名获取到这个对象-->
<alias name="user" alias="adfadjfa" />
5.2 Bean的配置
<!--
id:beand的唯一标识符,也就是相当于我们学的对象名
class:bean 对象所对应的全限定名 : 包名+ 类型
name : 也是别名,而且name可以取多个别名
-->
<bean id="userT" class="com.sheng.pojo.UserT" name="user2,u2 u3;u4">
<property name="name" value="13" />
</bean>
5.3 import
这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并为一个
假如,现在项目中有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的!
-
张三
-
李四
-
王五
-
applicationContext
<import resource="beans.xml" /> <import resource="beans2.xml" /> <import resource="beans3.xml" />使用的时候,直接使用总的配置就可以了
6、依赖(DI)注入
6.1、构造器注入
6.2、Set方式注入(重点)
-
依赖注入:Set注入!
-
依赖:bean对象的创建依赖于容器
-
注入:bean对象中的所有属性,由容器注入
【环境搭建】
-
复杂类型
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } } -
真实测试对象
import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Student { private String name; private Address address; private String books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private String wife; private Properties info; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getBooks() { return books; } public void setBooks(String books) { this.books = books; } public List<String> getHobbys() { return hobbys; } public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public String getWife() { return wife; } public void setWife(String wife) { this.wife = wife; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books='" + books + '\'' + ", hobbys=" + hobbys + ", card=" + card + ", games=" + games + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
-
-
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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.sheng.pojo.Student"> <!--第一种,普通至注入,value--> <property name="name" value="123"/> </bean> </beans> -
测试类
import com.sheng.pojo.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); } }
完善注册信息
<bean id="address" class="com.sheng.pojo.Address" />
<bean id="student" class="com.sheng.pojo.Student">
<!--第一种,普通至注入,value-->
<property name="name" value="123"/>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address" />
<!--第三种,数组注入,ref-->
<property name="books">
<array>
<value>水浒传</value>
<value>三国演义</value>
<value>红楼梦</value>
<value>西游记</value>
</array>
</property>
<!--list注入-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="123123123123123123"/>
<entry key="银行卡" value="1231231231231231231"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--NULL-->
<property name="wife">
<null />
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="driver">123123</prop>
<prop key="url">www.baidu.com</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
6.3、 拓展方式注入
我们可以使用P命名空间和C命名空间进行注入
官方解释:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--P命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.sheng.pojo.User" p:name="sheng" p:age="21" />
<!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.sheng.pojo.User" c:age="18" c:name="xinfan" />
</beans>
测试:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user2", User.class);
System.out.println(user);
}
注意点:p命名和c命名空间不能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
6.4 Bean Scopes(作用域)
-
Singleton Scope单例模式(Spring默认机制)
<bean id="user2" class="com.sheng.pojo.User" c:age="18" c:name="xinfan" scope="singleton"/> -
(Prototy Scope)原型模式:每次从容器中get的时候,都会产生一个新的对象
<bean id="user2" class="com.sheng.pojo.User" c:age="18" c:name="xinfan" scope="prototype"/> -
其余的request、session、application、这些个只能在web开发中使用到
7.Bean的自动装配(Autowired)
- 自动装配式Spring满足bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给bean装备属性
在Spring中有三种装配的方式
- 在xml中显示的配置
- 在java中显示配置
- 隐式的自动装配bean 【重要】
7.1 测试
环境搭配:一个人有两个宠物!
7.2 ByName自动装配
<!--
byName:会自动在容器上下文查找,和自己对象set方法后面的值对应的beanid!
-->
<bean id="people" class="com.sheng.pojo.People" autowire="byName">
<property name="name" value="xiaofan" />
<!--<property name="dog" ref="dog" />-->
<!--<property name="cat" ref="cat" />-->
</bean>
7.3ByType自动装配
<bean class="com.sheng.pojo.Cat" />
<bean class="com.sheng.pojo.Dog" />
<!--
byType:会自动在容器上下文查找,和自己对象属性类型相同的bean!
-->
<bean id="people" class="com.sheng.pojo.People" autowire="byType">
<property name="name" value="xiaofan" />
</bean>
小结:
- byname的时候,需要保证有所bean的id唯一,并且这个bean需要和自动注入的属性的set方法值一致
- bytype的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
7.4 使用注解实现自动装配
jdk1.5支持的注解,spring2.5支持的注解
要使用注解须知:
-
导入约束:context约束
-
==配置注解的支持:context:annotation-config/ [重要!]
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> </beans>
@Autowired
直接在属性上使用即可
使用Autowired我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byname!
科普:
@Nullable 字段标记了这个注解,说明这个字段可以为null;
public @interface Autowired {
boolean required() default true;
}
测试代码
public class People {
//如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
@Autowired(required = false)
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、我们可以使用@Qualifier(value="xxx")去配置@Autowired的使用,指定一个唯一的bean对象注入!
public class People {
@Qualifier(value ="cat111")
@Autowired
private Cat cat;
@Autowired
@Qualifier(value ="dog222")
private Dog dog;
private String name;
}
@Resource注解
public class People {
@Resource(name = "cat2")
private Cat cat;
@Resource(name = "dog2")
private Dog dog;
private String name;
}
小结:
@Resouce和@Autowired的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过bytype的方式实现【常用】
- @Resouce默认通过byname的方式实现,如果找不到名字,则通过bytype实现!如果两个都找不到的情况下,就报错!【常用】
- 执行顺序不同:@Autowired通过bytype的方式实现。@Resouce默认通过byname的方式实现。

浙公网安备 33010602011771号