初识spring及IOC理论、创建spring程序、spring的依赖注入(DI)、Bean的作用域、自动化装配Bean、使用注解进行自动装配

1、初识spring及IOC理论

spring是一个轻量级的IOC(控制反转)和面向切口(AOP)的框架

1.1、什么是IOC控制反转?

我们原来的方式写一个dao层和servie的代码:
1)dao层的接口

public interface UserDao {
	void getUser();
}

2 ) dao层的实现类

public class UserDaoImpl implements UserDao {
	public void getUser() {
		System.out.println("获取数据");
	}
}

3)service层的接口

public interface UserService {
	void getUserService();
}

4)service层的实现类

public class UserSerivceImpl implements UserService {
	private UserDao userDao = new UserDaoImpl();

	public void getUserService() {
		userDao.getUser();
	}
}

5)测试

@Test
	public void test(){
		UserSerivceImpl userSerivce = new UserSerivceImpl();
		userSerivce.getUserService();
	}

这样可以完成程序,如果我们要添加一个功能,那么就需要新创建一个UserDao的实现类

public class UserDaoMySql implements UserDao {
	public void getUser() {
		System.out.println("MySql读取数据");
	}
}

然后在service中去修改实现类

public class UserSerivceImpl implements UserService {
	private UserDao userDao = new UserDaoMySql();

	public void getUserService() {
		userDao.getUser();
	}
}

这个时候我们发现,如果要新增很多的功能的话,程序将会特别的臃肿,不方便操作,那么我们在service层中的实现类中添加一个set方法,用户需要添加什么功能,直接在set方法中传递参数就可以了。这样我们就把程序的主动权从我们手里交给了用户。

	@Test
	public void test(){
		UserSerivceImpl userSerivce = new UserSerivceImpl();
		userSerivce.setUserDao(new UserDaoMySql());
		userSerivce.getUserService();
	}
1.2、控制反转的本质

一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。

2、创建spring程序

2.1、利用maven导入spring所需要的jar包
 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
</dependency>
2.2、编写代码
  • 使用无参构造生成对象
    1)实体类
public class Hello {
	private String name;
	
	public Hello(){
		
	}

	public Hello(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Hello{" +
				"name='" + name + '\'' +
				'}';
	}
}

2)在resources文件夹中生成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 http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--id是对象名,class是类的路径,property是成员变量赋值-->
    <bean id="hello" class="hellosp.Hello">
        <property name="name" value="spring!"/>
    </bean>
</beans>

3)测试

	@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Hello hello = (Hello) context.getBean("hello");
		System.out.println(hello.toString());
	}
  • 使用有参构造生成对象
    1)根据构造方法中参数的名字来传递值
<bean id="hello" class="hellosp.Hello">
        <constructor-arg name="name" value="spring!"/>
    </bean>

2)根据构造方法中参数的坐标来传递值

<bean id="hello" class="hellosp.Hello">
        <constructor-arg index="0" value="spring!"/>
    </bean>

3)根据构造方法中参数的类型来传递值

<bean id="hello" class="hellosp.Hello">
        <constructor-arg type="java.lang.String" value="spring!"/>
    </bean>

3、spring的依赖注入(DI)

3.1、什么是DI?
  • 依赖:Bean对象的创建依赖于容器
  • 注入:Bean对象所依赖的资源,由容器来配置和装配
  • 依赖注入分为构造器注入、set方法注入、pname和cname注入
3.1、set方法注入
  • 使用set方法注入的属性必须含有set方法,并且方法名为set+属性名(首字母大写)
    1)实体类
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 void setName(String name) {
		this.name = name;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public void setBooks(String[] books) {
		this.books = books;
	}

	public void setHobbys(List<String> hobbys) {
		this.hobbys = hobbys;
	}

	public void setCard(Map<String, String> card) {
		this.card = card;
	}

	public void setGames(Set<String> games) {
		this.games = games;
	}

	public void setWife(String wife) {
		this.wife = wife;
	}

	public void setInfo(Properties info) {
		this.info = info;
	}

	@Override
	public String toString() {
		return "Student{" +
				"name='" + name + '\'' +
				", address=" + address +
				", books=" + Arrays.toString(books) +
				", hobbys=" + hobbys +
				", card=" + card +
				", games=" + games +
				", wife='" + wife + '\'' +
				", info=" + info +
				'}';
	}
}
public class Address {
	private Address address;

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
}

2)注入字符串常量

<bean id="stu1" class="setDI.Student">
        <property name="name" value="Henry"/>
</bean>

3)注入Bean,因为address是一个对象,所以要先在xml文档中生成address的bean

    <!--注入address-->
    <bean id="address" class="setDI.Address">
        <property name="address" value="陕西宝鸡"/>
    </bean>

    <!--stu对象中添加address-->
    <bean id="stu1" class="setDI.Student">
        <property name="name" value="Henry"/>
        <property name="address" ref="address"/>
        <property name="books">
            <array>
                <value>语文</value>
                <value>数学</value>
                <value>英语</value>
            </array>
        </property>
        <!--list类型-->
        <property name="hobbys">
            <list>
                <value>打游戏</value>
                <value>看电影</value>
            </list>
        </property>
        <!--map类型-->
        <property name="card">
            <map>
                <entry key="陕西" value="西安"/>
                <entry key="甘肃" value="兰州"/>
            </map>
        </property>
        <!--set类型-->
        <property name="games">
            <set>
                <value>DNF</value>
                <value>CF</value>
            </set>
        </property>
        <!--其它类型-->
        <property name="info">
            <props>
                <prop key="姓名">Henry</prop>
                <prop key="学号">123</prop>
            </props>
        </property>
        <!--值为null-->
        <property name="wife">
            <null/>
        </property>
    </bean>
3.3、p命名空间注入

实体类中没有构造方法

public class User {
	private String name;
	private int age;

	public void setName(String name) {
		this.name = name;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}
}
  • 使用p命名空间注入前要先导入约束:xmlns:p=“http://www.springframework.org/schema/p”
<bean id="user" class="setDI.User" p:name="Henry" p:age="22"/>
  • 我们发现在bean中没有使用构造方法就给对象赋值了,本质上是bean调用的setXXX方法
3.4、c命名空间注入
  • c命名空间注入本质上是利用构造方法传参来创建对象,如果没有有参构造方法,则会报错
<bean id="user2" class="setDI.User" c:name="Henry2" c:age="20"/>

4、Bean的作用域

4.1、singleton
  • 当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。要在XML中将bean定义成singleton,可以这样配置:
<bean id="user" class="setDI.User" c:name="Henry2" c:age="20" scope="singleton"/>

验证测试:

@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		User user1 = (User) context.getBean("user");
		User user2 = (User) context.getBean("user");
		System.out.println(user1==user2);
	}

最后结果为true

4.2、prototype
  • 当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,可以这样配置:
  <bean id="user" class="setDI.User" c:name="Henry2" c:age="20" scope="prototype"/>

验证测试:

@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		User user1 = (User) context.getBean("user");
		User user2 = (User) context.getBean("user");
		System.out.println(user1==user2);
	}

最后结果为false

5、自动化装配Bean

5.1、搭建测试所需要的类

1)实体类

public class Eat {
	public void eat(){
		System.out.println("咥面");
	}
}
public class Sleep {
	public void sleep(){
		System.out.println("睡席梦思");
	}
}
public class People {
	private Eat eat;
	private Sleep sleep;
	private String name;

	public void setEat(Eat eat) {
		this.eat = eat;
	}

	public void setSleep(Sleep sleep) {
		this.sleep = sleep;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Eat getEat() {
		return eat;
	}

	public Sleep getSleep() {
		return sleep;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "People{" +
				"eat=" + eat +
				", sleep=" + sleep +
				", name='" + name + '\'' +
				'}';
	}
}

2)编写xml

  <bean id="people" class="autobean.People">
        <property name="eat" ref="eat"/>
        <property name="sleep" ref="sleep"/>
        <property name="name" value="Henry"/>
  </bean>

3)测试结果

	@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		People people = (People) context.getBean("people");
		System.out.println(people.getName());
		people.getEat().eat();
		people.getSleep().sleep();
	}
5.2、byName
  • byName是根据autowire byName按照id的名称进行自动装配。具体步骤为:
    • 查找类中的setXXX方法,例如setEat(),然后得到去掉set的字符串即eat
    • 再id中寻找与eat相同的bean对象
    • 如果有,就注入,没有则报空指针异常
 <bean id="person" class="autobean.People" autowire="byName">
        <property name="name" value="Henry"/>
    </bean>
    因为上文中的eat对象和sleep对象已经配置完成了,所以会自动配置
5.3、byType
  • byType是按照类型进行匹配
<bean id="person" class="autobean.People" autowire="byType">
        <property name="name" value="Henry"/>
</bean>

6、使用注解进行自动装配

  • 使用@Autowire
    实体类
public class People {
	@Autowired
	private Eat eat;
	@Autowired
	private Sleep sleep;
	private String name;

	public void setEat(Eat eat) {
		this.eat = eat;
	}

	public void setSleep(Sleep sleep) {
		this.sleep = sleep;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Eat getEat() {
		return eat;
	}

	public Sleep getSleep() {
		return sleep;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "People{" +
				"eat=" + eat +
				", sleep=" + sleep +
				", name='" + name + '\'' +
				'}';
	}
}
class Test4{
	@Test
	public void test(){
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		People people = (People) context.getBean("person");
		System.out.println(people.getName());
		people.getEat().eat();
		people.getSleep().sleep();
	}
}
  • xml配置文件
<bean id="eat2" class="autobean.Eat"/>
    <bean id="sleep2" class="autobean.Sleep"/>
<bean id="person" class="autobean.People" p:name="Henry"/>
  • 使用@Quaifier,根据byName进行装配,不能单独使用,需要和根据类型进行装配的@Autowire配合使用
<bean id="eat2" class="autobean.Eat"/>
    <bean id="eat3" class="autobean.Eat"/>
    <bean id="sleep2" class="autobean.Sleep"/>
    <bean id="sleep3" class="autobean.Sleep"/>
    <bean id="person" class="autobean.People" p:name="Henry"/>
  • 此时,只是用@Quaifier会报错,需要配合@Autowire使用
@Autowired
	@Qualifier(value = "eat2")
	private Eat eat;
	@Autowired
	@Qualifier(value = "sleep2")
	private Sleep sleep;
  • @Resource如有指定的name属性,先按该属性进行byName方式查找装配;其次再进行默认的byName方式进行装配;如果以上都不成功,则按byType的方式自动装配。都不成功,则报异常。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值