【Spring5】2 - IOC容器概念、应用、方法,基于xml和注解

IOC概念和原理

IOC概念

IOC(Inversion of control,控制反转)

  • 将对象创建、对象之间的调用过程,交给Spring进行管理
  • 使用IOC的目的:降低耦合度

IOC 入门案例: https://blog.csdn.net/CherryChenieth/article/details/123043600?spm=1001.2014.3001.5501

IOC底层原理: xml解析 + 工厂模式 + 反射

工厂模式介绍
在这里插入图片描述

如果用户想要获得一个圆形,只需要告诉工厂,我想要一个“Cicle”,而不需要理解建造圆形的具体细节。从代码上说,客户端只需要调用Shape shape = shapeFactory.getShape("Circle");就可以获得一个Circle对象了,而不需要显式地使用Circle circle = new Circle();
使用工厂模式可以使得类之间的耦合度降低。

控制反转(IOC)是设计模式中的一种原则,在Java中的体现就是“反射”。通俗的说,反射就是根据给出的类名(字符串)来生成对象。这种编程方式可以让应用在运行时才动态决定生成哪一种对象。

结合工厂模式+反射,IOC就是利用一个工厂类,通过运行时动态获取需要的对象,要求工厂类生成相应的对象。

代码示例

第一步:在xml配置文件中配置创建的对象

<bean id="dao" class="com.cherrychen.UserDao"></bean> 

第二步:已有service类和dao类,创建工厂类

class UserFactory{
	public static UserDao getDao(){
		String classValue = class属性值; //1. 这里就是通过xml解析得到的class字符串"com.cherrychen.UserDao"
		Class clazz = Class.forName(classValue); // 2. 通过反射创建对象(用一个字符串解析类型,创建该类型的对象)
		return (UserDao)clazz.newInstance(); //3. 返回创建好的UserDao对象,要生成什么对象就枪强转成什么类型
	}
}

IOC的BeanFactory接口

  1. IOC思想基于IOC容器完成,IOC容器底层就是对象工厂
  2. Spring提供的IOC容器有两种实现方式(两个接口)
    (1)BeanFactory:IOC容器基本实现,是Spring内部的使用接口,不提供给开发人员使用
  • 加载配置文件时不会创建对象,在获取对象时才创建对象
    (2)ApplicationContext:BeanFactory的子接口,提供更多更强大的功能,一般由开发人员进行使用
  • 加载配置文件时就会创建配置文件指定的对象
  • 耗时耗资源的操作一般是启动时就加载,所以一般使用第二种

在idea中Ctrl + H查看ApplicationContext的接口实现类:
在这里插入图片描述

IOC操作Bean管理

概念

  1. Bean管理:Spring创建对象 + 注入属性
  2. 实现Bean管理的两种方式:
  • 基于xml配置文件方式实现
  • 基于注解方式实现

基于XML方式

  1. XML创建对象
  2. XML注入属性
  3. XML注入其他类型属性
  4. XML注入集合属性
  5. XML:FactoryBean-Bean作用域-Bean生命周期
  6. XML自动装配
  7. XML外部属性文件

IOC操作bean管理 - 基于XML方式

基于XML方式创建对象

<bean id="User" class="com.cherrychen.User"></bean> 

(1)在Spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
(2)在bean标签中有很多属性:
* id:唯一标识
* class:类全路径(包类路径)
(3)创建对象的时候,默认执行无参数构造方法完成对象创建。

基于XML方式注入属性

Dependency Injection:依赖注入(注入属性)

第一种注入方式:使用set方法进行注入

(1) 创建类,定义属性和set方法

public class Book {
    // 创建属性
    private String bname;
    private String bauthor;
    
    //创建属性对应的set方法
    public void setBname(String bname) {
        this.bname = bname;
    }
    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

	// get方法用于测试
	public String getBname() {
        return bname;
    }
    public String getBauthor() {
        return bauthor;
    }
}

(2) 新建一个spring配置文件bean.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">
    <!--    2. set方法注入属性    -->
    <!--    配置Book对象    -->
    <bean id="book" class="com.cherrychen2.Book">
        <!--使用property完成属性注入
                name:类里面属性的名称
                value:向属性注入的值
        -->
        <property name="bname" value="大话设计模式"> </property>
        <property name="bauthor" value="数据结构预算法"></property>
    </bean>
</beans>

(3)测试

public class IOCTest {
    @Test
    public void test(){
        //1。获取配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        Book book = context.getBean("book", Book.class);
        System.out.println(book.getBauthor());
        System.out.println(book.getBname());
    }
}

测试结果:
在这里插入图片描述

第二种注入方式:使用有参数构造进行注入

(1)创建类,定义属性,创建属性对应有参数构造方法

public class Orders {
    //属性
    private String oname;
    private String address;
    
    //有参构造
    public Orders(String oname, String address) {
        this.oname = oname;
        this.address = address;
    }

    //get方法用于测试
    public String getOname() {
        return oname;
    }
    public String getAddress() {
        return address;
    }
}

(2) 在Spring配置文件中进行配置

<?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">
               
    <!--    3 有参数构造注入属性     -->
    <bean id="orders" class="com.cherrychen2.Orders">
        <constructor-arg name="oname" value="MacbookPro"></constructor-arg>
        <constructor-arg name="address" value="China"></constructor-arg>
    </bean>
    
</beans>

(3) 测试

public class IOCTest {
    @Test
    public void test2(){
        //1。获取配置文件
        ApplicationContext context= new ClassPathXmlApplicationContext("beanOrder.xml");

		//2. 创建对象
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println(orders);
    }
}

测试结果
在这里插入图片描述

p名称空间注入:使用p名称空间注入,可以简化xml配置方式

第一步:添加p名称空间在配置文件中 xmlns:p="http://www.springframework.org/schema/p"

第二步:进行属性注入,在bean标签里面进行操作

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--    2. set方法注入属性    -->
    <!--    p名称空间注入     -->
    <bean id="book" class="com.cherrychen2.Book" p:bname="数据结构" p:bauthor="无名氏"></bean>
</beans>

xml注入其他类型属性

字面量

(1)null值

<!--null-->
<property name="address">
	<null/>
</property>

(2)属性值包含特殊符号

<!--属性值包括特殊符号
	1. 把<>进行转义:&lt; &gt;
	2. 把带特殊符号的内容写到CDATA
-->

<property name="address">
	<value><![CDATA[<<樱桃>>]]></value>
</property>

注入属性:外部bean
1. 创建两个类service和dao
2. 在service类中调用dao的方法
3. 在Spring配置文件中进行配置

/**
* UserDao接口
*/
public interface UserDao {
    void update();
}

/**
* UserDao接口的实现类
*/
public class UserDaoImpl implements UserDao{
    @Override
    public void update() {
        System.out.println("DAO: updating...");
    }
}

/**
* 包含UserDao类的UserService类
*/
public class UserService {
    // 创建UserDao类型属性,生成set方法
    private UserDao userDao;
    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("service...+++");
        userDao.update();
    }
}

在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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">
               
    <!--    1。 创建service和dao对象      -->
    <bean id="userService" class="com.cherrychen2.UserService">
        <!--    注入userDao对象
                    name:类里面属性名称
                    ref:创建userDao对象bean标签的id值-->
        <property name="userDao" ref="userDaoImpl"></property>
    </bean>
    
    <bean id="userDaoImpl" class="com.cherrychen2.UserDaoImpl"></bean>
    
</beans>

注入属性-内部bean
(1)一对多关系:部门和员工
一个部门 有多个员工,一个员工属于一个部门
部门是一,员工是多
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示

//部门类
public class Dept {
    private String dname;
    public void setDname(String dname) {
        this.dname = dname;
    }
}

//员工类
public class Employee {
    private String ename;
    private String gender;
    //员工属于一个部门。使用对象形式表示
    private Dept dept;

    //set方法
    public void setEname(String ename) {
        this.ename = ename;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
}

(3)在Spring配置文件中进行配置

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--    内部bean      -->
    <bean id="employee" class="com.cherrychen3.Employee">
        <!--设置两个普通属性-->
        <property name="ename" value="Cherry Chen"></property>
        <property name="gender" value="female"></property>
        <!--设置对象类型属性-->
        <property name="dept">
            <bean id="dept" class="com.cherrychen3.Dept">
                <property name="dname" value="IT部门"></property>
            </bean>
        </property>
    </bean>
</beans>

注入属性:级联赋值

第一种写法:向多个实体类中设置属性值

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--    级联赋值    -->
    <bean id="employee" class="com.cherrychen3.Employee">
        <!--设置两个普通属性-->
        <property name="ename" value="Cherry Chen"></property>
        <property name="gender" value="female"></property>
        <!--设置对象类型属性-->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.cherrychen3.Dept">
        <property name="dname" value="IT部门"></property>
    </bean>
    
</beans>

第二种写法:生成内部bean那个类的get方法

在这里插入图片描述

在原来的bean中,可以用dept.dname的方式修改该属性。
在这里插入图片描述

XML注入集合属性

(1) 创建类,定义数组、list、map、set类型属性,生成对应的set方法

public class Student {
    // 1. 数组类型属性
    private String[] courses;
    // 2. list集合类型属性
    private List<String> list;
    // 3. map集合类型属性
    private Map<String, String> maps;
    // 4. set集合类型属性
    private Set<String> sets;

    //set 方法
    public void setCourses(String[] courses) {
        this.courses = courses;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }

    @Override
    public String toString() {
        return "Student{" +
                "courses=" + Arrays.toString(courses) +
                ", list=" + list +
                ", maps=" + maps +
                ", sets=" + sets +
                '}';
    }
}

(2) 在Spring配置文件中配置

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="student" class="com.cherrychen3.Student">
        <!--    数组类型属性注入    -->
        <property name="courses">
            <array>
                <value>Java课程</value>
                <value>C++课程</value>
            </array>
        </property>

        <!--    list类型属性注入    -->
        <property name="list">
            <list>
                <value>Cherry</value>
                <value>Milanda</value>
            </list>
        </property>

        <!--    map类型属性注入    -->
        <property name="maps">
            <map>
                <entry key="JAVA" value="java lang"></entry>
                <entry key="PHP" value="php"></entry>
            </map>
        </property>

        <!--    set类型属性注入    -->
        <property name="sets">
            <set>
                <value>MongoDB</value>
                <value>MySQL</value>
            </set>
        </property>
    </bean>

</beans>
在集合中设置对象类型值
    <!--    创建多个course对象    -->
    <bean id="course1" class="com.cherrychen3.Course">
        <property name="cname" value="Spring5"></property>
    </bean>
    <bean id="course2" class="com.cherrychen3.Course">
        <property name="cname" value="MyBatis"></property>
    </bean>

    <bean id="student" class="com.cherrychen3.Student">

        <!--    list类型属性注入    -->
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
把集合注入部分提取出来

(1)在Spring配置文件中引入名称空间util

<?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:util="http://www.springframework.org/schema/util"  
       xsi:schemaLocation=
              "http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/util 
               http://www.springframework.org/schema/util/spring-util.xsd">

在这里插入图片描述

(2) 使用util标签完成list集合注入提取

    <util:list id="bookList">
        <value>数据结构与算法</value>
        <value>乌合之众</value>
        <value>我们时代的神经症人格</value>
    </util:list>
    
    <!--    提取list集合类型属性注入使用    -->
    <bean id="book" class="com.cherrychen2.Book">
        <property name="list" ref="bookList"></property>
    </bean>

FactoryBean

  1. Spring有两种类型bean:普通bean和工厂bean
  2. 普通bean:在配置文件中定义的bean类型就是返回类型
  3. 工厂bean:在配置文件中定义的bean类型可以和返回类型不一样
    第一步:创建类,让这个类作为工厂bean,实现接口FactoryBean
    第二步:实现接口里面的方法,在实现的方法中定义返回的bean类型
public class MyBean implements FactoryBean<Course> {
    //定义返回的bean
    @Override
    public Course getObject() throws Exception {
        Course course = new Course();
        course.setCname("MySQL-course name");
        return course;
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}

xml中配置:

<bean id="myBean" class="com.cherrychen4.MyBean"></bean>

测试:

    @Test
    public void test5(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beanfactory.xml");
        Course course = context.getBean("myBean", Course.class);
        System.out.println(course);
    }

测试结果:
在这里插入图片描述

bean的作用域

  1. 在Spring里,设置创建bean实例是单实例还是多实例
  2. 在Spring里,默认情况下,bean是单实例对象

在这里插入图片描述

  1. 如何设置单实例还是多实例
    (1)在Spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例
    (2)scope属性值
    第一个值:默认值,singleton,表示是单实例对象
    第二个值:prototype,表示是多实例对象
    bean.xml改为:<bean id="book" class="com.cherrychen2.Book" scope="prototype" p:bname="数据结构" p:bauthor="无名氏"></bean>
    在这里插入图片描述

bean的生命周期

生命周期:对象从创建到销毁的过程

bean的生命周期(未加后置处理器)

  1. 通过无参数构造器创建bean实例
  2. 为bean的属性设置值和对其他bean引用(调用set方法)
  3. 调用bean的初始化方法(需要进行配置初始化的方法)
  4. bean可以使用了(对象获取到了)
  5. 当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)
public class Orders {
    private String oname;

    public Orders() {
        System.out.println("第一步 执行无参数构造创建bean实例");
    }

    public void setOname(String oname) {
        this.oname = oname;
        System.out.println("第二步 调用set方法设置属性值");
    }
    
    //创建执行的初始化的方法
    public void initMethod(){
        System.out.println("第三步 执行初始化的方法");
    }
    
    //创建执行的销毁的方法
    public void destroyMethod(){
        System.out.println("第五步 执行销毁的方法");
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="orders" class="com.cherrychen4.Orders" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="手机"></property>
    </bean>

</beans>

测试:

    @Test
    public void test7(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beanOrder.xml");
        var orders = context.getBean("orders", com.cherrychen4.Orders.class);
        System.out.println("第四步:获取创建bean实例对象");
        System.out.println(orders);
        //手动让bean实例销毁
        context.close(); // .close()会调用destroy-method
    }

测试结果
在这里插入图片描述

bean的生命周期(带后置处理器)

  1. 通过无参数构造器创建bean实例
  2. 为bean的属性设置值和对其他bean引用(调用set方法)
  3. 把bean实例传递bean后置处理器的方法postProcessBeforeInitialization
  4. 调用bean的初始化方法(需要进行配置初始化的方法)
  5. 把bean实例传递bean后置处理器的方法postProcessAfterInitialization
  6. bean可以使用了(对象获取到了)
  7. 当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)
public class MyBeanPost implements BeanPostProcessor { 
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
			System.out.println("在初始化之前执行的方法");
	        return bean;
    }
    @Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("在初始化之后执行的方法");
        return bean;
    }
}

XML自动装配(用得少,一般用注解)

什么是自动装配:根据指定装配规则(属性名称或属性类型),Spring自动将匹配的属性值进行注入

(1)根据属性名称自动注入

<!--实现自动装配
	bean标签属性autowire,配置自动装配
	autowire属性值通常有两个值:
		byName  根据属性名称注入,注入值bean的id值和类属性名称一样
		byType  根据属性类型注入
-->
<bean id="Employee" class="com.cherrychen4.autowire.Employee" autowire="byName"></bean>
<bean id="dept" class="com.cherrychen4.autowire.Dept"></bean>

外部属性文件(如数据库用户权限验证)

方法一: 直接配置数据库信息
(1)配置德鲁伊连接池
(2)引入德鲁伊连接池依赖jar包
在这里插入图片描述

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

方法二:引入外部属性文件配置数据库连接池

(1)创建外部属性文件,properties格式文件,写数据库信息

在这里插入图片描述

(2) 把外部properties属性文件引入到Spring配置文件中

  • 引入context名称空间
    xmlns:util="http://www.springframework.org/schema/util"
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
<?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:util="http://www.springframework.org/schema/util"
       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/util
               http://www.springframework.org/schema/util/spring-util.xsd
               http://www.springframework.org/schema/context 
               http://www.springframework.org/schema/context/spring-context.xsd" >
  • 在Spring配置文件使用标签引入外部属性文件,使用${xxx}的方式设置value
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClassName}"></property>
        <property name="url" value="${prop.url}"></property>
        <property name="username" value="${prop.username}"></property>
        <property name="password" value="${prop.password}"></property>
    </bean>

IOC操作Bean管理 - 基于注解方式

  1. 什么是注解
  • 注解是代码的特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值,…)
  • 使用注解,注解作用在类、方法、属性上面
  • 使用注解的目的:简化xml配置
  1. Spring针对bean管理中创建对象提供注解:
  • @Component
  • @Service
  • @Controller
  • @Repository
    上面四个注解的功能是一样的,都可以用来创建bean实例。
  1. 基于注解方式实现对象创建
    第一步 引入jar包:spring-aop-5.2.6.RELEASE.jar

第二步 开启注解扫描

    <!--    开启组件扫描
                1,如果扫描多个包,包名用逗号隔开 "com.cherry.dao, com.chery.service"
                2,扫描包上层目录
    -->
    <context:component-scan base-package="com.cherrychen4"/>

第三步 创建类,在类上面添加【创建对象】的注解

@Component(value = "userService") // <bean id="userService" class="...">
public class UserService {
    // 创建UserDao类型属性
    private UserDao userDao;

    public void add(){
        System.out.println("service...+++");
        userDao.update();
    }
}

第四步 开启组件扫描的细节配置

    <!--include表示只扫描@Controller, exclude则不扫描-->
    <context:component-scan base-package="com.cherrychen2" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

第五步 基于注解方式实现属性注入
(1)@Autowired:根据属性类型进行自动装配

@Service(value = "userService") // <bean id="userService" class="...">
public class UserService {
    // 创建UserDao类型属性
    // 不需要添加set方法
    // 添加注入属性的注解@autowired
    @Autowired
    private UserDao userDao;

    public void add(){
        System.out.println("service...+++");
        userDao.update();
    }
}

(2) @Qualifier:根据名称进行注入
需要和@Autowired一起使用

@Service(value = "userService") // <bean id="userService" class="...">
public class UserService {
    // 创建UserDao类型属性
    // 不需要添加set方法
    // 添加注入属性的注解@autowired
    @Autowired //根据类型进行注入
    @Qualifier(value = "userDaoImpl1") //根据名称进行注入,放入不同的实现类
    private UserDao userDao;

    public void add(){
        System.out.println("service...+++");
        userDao.update();
    }
}

(3)@Resource:可以根据类型注入,也可以根据名称注入**【基于javax,不推荐使用】**

public class UserService {
    @Resource //根据类型进行注入
    @Resource(value = "userDaoImpl1") //根据名称进行注入,放入不同的实现类
    private UserDao userDao;

    public void add(){
        System.out.println("service...+++");
        userDao.update();
    }
}

(4)@Value:注入普通类型属性

@Value(value = "abc")
private String name;

完全注解开发

(1)创建配置类,代替xml文件

@Configuration //作为配置类,代替xml文件
@ComponentScan(basePackages = {"com.cherrychen"})
public class SpringConfig{
}

(2) 编写测试类

    @Test
    public void test8(){
        //加载配置类
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值