Spring笔记
Spring容器
控制反转
控制:传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的.
反转:程序本身不创建对象,而变成被动的接受对象.(传参的方式)
依赖注入:就是利用set方法来进行注入的.
IOC是一种编程思想,有主动的编程变成被动的接受
初始结构
//dao层实现接口,比如有两个实现类
public class UserDaoImpl implements UserDao{
@Override
public void getUser() {
System.out.println("admin");
}
}
public class UserDaoMySQL implements UserDao{
@Override
public void getUser() {
System.out.println("mysql连接");
}
}
//业务层实现类
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void getUser() {
userDao.getUser();
}
}
//测试类
public class MyTest {
public static void main(String[] args) {
//拿到Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我们的对象现在都在Spring中,取出来就行
UserServiceImpl userService = (UserServiceImpl) context.getBean("userServiceImpl");
userService.getUser();
}
}
容器管理,对象由String创建,管理,装配
<!--使用Spring创建对象,在Spring这些都称为Bean
类型 变量名 = new Hello()
id = 变量名
class = new的对象
property 相当于给对象的属性设置一个值-->
<bean id="hello" class="com.zdh02.pojo.Hello">
<!--value:设置一个基本数据类型的值-->
<property name="name" value="Spring"/>
</bean>
<bean id="mysqlImpl" class="com.zdh.dao.UserDaoMySQL"></bean>
<bean id="impl" class="com.zdh.dao.UserDaoImpl"></bean>
<bean id="userServiceImpl" class="com.zdh.service.UserServiceImpl">
<!--ref:引用Spring容器种已经创建好的对象-->
<property name="userDao" ref="impl"></property>
</bean>
IOC创建对象的方式
public class Demo1 {
private String name;
private int age;
public Demo1(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show() {
System.out.println("name=" + name + " " + "age=" + age);
}
}
//测试方法
@Test
public void Test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Demo1 demo1 = (Demo1) context.getBean("demo1");
demo1.show();
}
public class Demo2 {
private String name;
private int age;
public Demo2() {
System.out.println("初始化成功");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show() {
System.out.println("name=" + name + " " + "age=" + age);
}
}
这里Demo2也会被初始化
<bean id="demo1" class="com.zdh02.pojo.Demo1">
<constructor-arg name="name" value="赵德汉"></constructor-arg>
<constructor-arg name="age" value="19"></constructor-arg>
</bean>
<bean id="demo2" class="com.zdh02.pojo.Demo2"></bean>
在配置文件加载的时候,容器(span标签)中管理的对象就已经被创建了
Set方式注入(重要)
依赖注入:Set注入
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,由容器来注入
//拿几个类型举例注入
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
'}';
}
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;
}
}
<bean id="address" class="com.zdh.pojo.Address">
<property name="address" value="西安"/>
</bean>
<bean id="student" class="com.zdh.pojo.Student">
<property name="name" value="赵德汉"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>周防天音</value>
<value>风见一姬</value>
</array>
</property>
<property name="card">
<map>
<entry key="赵日天" value="庐山升龙霸"/>
<entry key="叶良辰" value="釜山无影脚"/>
</map>
</property>
<property name="hobbys">
<list>
<value>编码</value>
<value>听歌</value>
<value>二次元</value>
</list>
</property>
</bean>
//测试方法
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
c命名注入与p命名注入(扩展方式注入)
p命名注入其实就是Set注入
c命名注入是构造器注入,有参构造
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//测试
@Test
public void test2() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
User user = context.getBean("user2",User.class);
System.out.println(user.toString());
}
这俩需要导入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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名控件注入,可以直接注入属性的值:property-->
<bean id="user" class="com.zdh.pojo.User" p:name="赵德汉" p:age="12"/>
<!--c命名空间注入,通过构造器注入:construct-->
<bean id="user2" class="com.zdh.pojo.User" c:name="赵日天" c:age="20"/>
</beans>
bean的作用域
1.单例模式(Spring默认机制)
<bean id="user2" class="com.zdh.pojo.User" c:name="赵日天" c:age="20" scope="singleton"/>
2.原型模式:每次从容器中get的时候,都会产生一个新对象
<bean id="user2" class="com.zdh.pojo.User" c:name="赵日天" c:age="20" scope="prototype"/>
3.其余的request、session、application,这仨只能在web开发中使用
request:只在一次请求中有效
session:一次会话中有效
application:全局有效
bean自动装配
- byName的时候,需要保证bean的id唯一,并且这个bean需要和自动注入的属性和set方法的值一致.
- byType的时候,需要保证bean的class唯一,会自动在容器上下文查找,并且这个bean需要和自动注入的属性的类型一致.
//比如一个人有猫和狗
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
public class Dog {
public void shout(){
System.out.println("wang~");
}
}
public class People {
private Dog dog;
private Cat cat;
private String name;
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
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;
}
}
//测试
@Test
public void test3() {
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
People people = context.getBean("people",People.class);
System.out.println(people.getName());
people.getCat().shout();
people.getDog().shout();
}
<!--
byName:会自动在容器上下文查找,与自己对象set方法后面的值对应的beanid
byType:会自动在容器上下文查找,与自己对象属性类型相同的bean
-->
<bean class="com.zdh.pojo2.Cat"/>
<bean class="com.zdh.pojo2.Dog"/>
<bean id="people" class="com.zdh.pojo2.People" autowire="byType">
<property name="name" value="赵德汉"/>
</bean>
使用注解实现自动装配
1.导入约束
2.配置注解的支持
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Autowired
直接在属性上使用即可!也可以在set方式上使用!
使用Autowired可以不用编写Set方法,前提是自己的自动装配的属性在IOC容器中存在,且符合命名byname
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value = “xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入
@Autowired
@Qualifier(value = "dog22")
private Dog dog;
@Autowired
@Qualifier(value = "cat11")
private Cat cat;
@Resource
@Resource
private Dog dog22;
@Resource(name = "cat11")
private Cat cat;
注解说明
- @Autowired:自动装配通过类型。名字…如果Autowired不能唯一自动装配上属性,则须通过@Qualifier(value = “xxx”)
- @Resource:自动装配通过名字。类型。