Spring IOC

Spring

在这里插入图片描述

IOC

工厂模式

在这里插入图片描述

IOC过程

在这里插入图片描述

IOC接口

在这里插入图片描述

IOC操作Bean管理

1、什么是 Bean 管理

​ (0)Bean 管理指的是两个操作

​ (1)Spring 创建对象

​ (2)Spirng 注入属性

2、Bean 管理操作有两种方式

​ (1)基于 xml 配置文件方式实现

​ (2)基于注解方式实现

IOC操作Bean管理(XML)

1、基于 xml 方式创建对象
在这里插入图片描述
(1)在 spring 配置文件中,使用 bean 标签,标签里面添加对应属性,就可以实现对象创建

​ (2)在 bean 标签有很多属性,介绍常用的属性

			id 属性:唯一标识
			class 属性:类全路径(包类路径) ​			   

​ (3)创建对象时候,默认也是执行无参数构造方法完成对象创建

2、基于 xml 方式注入属性

​ (1)DI:依赖注入,就是注入属性

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

​ (1)创建类,定义属性和对应的 set 方法

public class Book {
    private String bname;
    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    public void Test(){
        System.out.println(bname+"///"+bauthor);
    }

}

配置文件
在这里插入图片描述

  • 测试代码
public class TestSpring5 {
    @Test
    public void testBook1(){
        //加载spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //获取配置创建的对象
        Book book = context.getBean("book",Book.class);
        
        System.out.println(book);
        book.Test();
    }
}

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

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

public class Orders {

    private String oname;
    private String address;

    public Orders(String oname, String address) {
        this.oname = oname;
        this.address = address;
    }

    public void Test(){
        System.out.println(oname+"///"+address);
    }
}

配置文件

在这里插入图片描述

public class TestSpring5 {   
    @Test
    public void testOrders(){
        //加载spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        //获取配置创建的对象
        Orders orders = context.getBean("orders", Orders.class);

        System.out.println(orders);
        orders.Test();
    }
}

5、p 名称空间注入(了解)

(1)使用 p 名称空间注入,可以简化基于 xml 配置方式 第一步 添加 p 名称空间在配置文件中

在这里插入图片描述
(2)第二步 进行属性注入,在 bean 标签里面进行操作

<bean id="book1" class="com.atguigu.spring.Book" p:bname="九阳神功" p:bauthor="无名氏" ></bean>

IOC 操作 Bean 管理(xml 注入其他类型属性)

1、字面量

​ (1)null 值

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

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

<!--属性包含特殊符号
    1.把《》进行特殊转译 &lt:<  &gt:>
    2.把带特殊符号内容写到CDATA
-->
    <property name="address">
        <value><![CDATA[<<南京>>]]></value>
    </property>

2、注入属性-外部 bean

(1)创建两个类 service 类和 dao 类 
(2)在 service 调用 dao 里面的方法 
(3)在 spring 配置文件中进行配置 
public interface UserDao {
    public void updata();

}
public class UserDaoImpl implements UserDao{

    @Override
    public void updata() {
        System.out.println("dao update ...........");
    }
}
public class UserService {
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("service add..............");
        //原始方式 创建UserDaod对象
        //UserDao UserDao = new UserDaoImpl();
        //UserDao.updata();
        userDao.updata();
    }
}
<?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">
     <!--创建UserServiceUserDao的对象-->
    <bean id="userservice" class="com.atguigu.spring.service.UserService">
        <!--注入UserDao的对象
            name 属性:类里面属性的名称
            ref  属性:创建UserDao对象bean标签的id
         -->
        <property name="userDao" ref="userdaoImpl"></property>
    </bean>
    <bean id="userdaoImpl" class="com.atguigu.spring.dao.UserDaoImpl"></bean>
</beans>

测试代码

public class TestBean {
    //加载spring配置文件
    @Test
     public void Test(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");

        //获取配置创建的对象
        UserService userService = context.getBean("userService",UserService.class);
        userService.add();
    }
}

3、注入属性-内部 bean

(1)一对多关系:部门和员工 一个部门有多个员工,一个员工属于一个部门 部门是一,员工是多 
(2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示  
public class Dept {
    private String dname;

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

    public void setDname(String dname) {
        this.dname = dname;
    }

}
//员工类
public class Emp {

    private String ename;
    private String gender;
    //员工属于哪个部门,用对象形式表示
    private Dept dept;

    public void setDept(Dept dept) {
        this.dept = dept;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public void add(){
        System.out.println(ename+" "+gender+" "+dept);
    }
}

<?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">
     <!--内部bean-->
    <bean id="emp" class="com.atguigu.spring.bean.Emp">
    <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value="女"></property>
    <!--设置对象类型属性-->
        <property name="dept">
            <bean id="dept" class="com.atguigu.spring.bean.Dept">
                <property name="dname" value="安保部"></property>
            </bean>
        </property>
    </bean>
</beans>  
//测试代码
public class TestBean {
    //加载spring配置文件
    @Test
    public void Test(){;
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
        //获取配置创建的对象
        UserService userService = context.getBean("userService",UserService.class);
        userService.add();
    }

    @Test
    public void Bean(){;
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean3.xml");
        //获取配置创建的对象
        Emp emp = context.getBean("emp",Emp.class);
        emp.add();
    }
}

4、注入属性-级联赋值

​ (1)第一种写法

<?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">
    <!--级联赋值-->
    <bean id="emp" class="com.atguigu.spring.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.atguigu.spring.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>
</beans>

​ (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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--级联赋值-->
    <bean id="emp" class="com.atguigu.spring.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value="女"></property>
        <!--级联赋值-->
        <property name="dept" ref="dept"></property>
        <property name="dept.dname" value="技术部"></property>
    </bean>
    <bean id="dept" class="com.atguigu.spring.bean.Dept"></bean>
</beans>

注意需要生成对应的get方法
在这里插入图片描述


IOC 操作 Bean 管理(xml 注入集合属性)

1、注入数组类型属性

2、注入 List 集合类型属性

3、注入 Map 集合类型属性

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

​ (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">
    <bean id="stu" class="com.atguigu.spring.collectiontype.Stu">
        <!--数组属性注入-->
        <property name="courses">
            <array>
                <value>java课程</value>
                <value>数据库课程</value>
            </array>
        </property>
        <!--list属性注入-->
        <property name="list">
            <list>
                <value>唐三</value>
                <value>小三</value>
            </list>
        </property>
        <!--map属性注入-->
        <property name="maps">
            <map>
                <entry key="JAVA" value="java"></entry>
                <entry key="PHP" value="php"></entry>
            </map>
        </property>
        <!--set属性注入-->
        <property name="set">
            <set>
                <value>MySQL</value>
                <value>Redis</value>
            </set>
        </property>
    </bean>

</beans>
public class Stu {
    private String[] courses;

    private List<String> list;

    private Map<String,String> maps;

    private Set<String> 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 setSet(Set<String> set) {
        this.set = set;
    }

    public void test(){
        System.out.println(Arrays.toString(courses));
        System.out.println(list);
        System.out.println(maps);
        System.out.println(set);
    }
}

public class TestSpring {
    @Test
    public void test() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
        Stu stu = context.getBean("stu", Stu.class);
        stu.test();
    }
}

4、在集合里面设置对象类型值

public class Course {
    private String cname;//课程名称

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Course{" +
                "cname='" + cname + '\'' +
                '}';
    }
}
 private List<Course> courseslist;

    public void setCourseslist(List<Course> courseslist) {
        this.courseslist = courseslist;
    }
 <!--注入List属性,但值是一个对象-->
        <property name="courseslist">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
    </bean>
    <!--创建多个course对象-->
    <bean id="course1" class="com.atguigu.spring.collectiontype.Course">
        <property name="cname" value="Spring5框架"></property>
    </bean>
    <bean id="course2" class="com.atguigu.spring.collectiontype.Course">
        <property name="cname" value="MyBatis框架"></property>
    </bean>

5、把集合注入部分提取出来

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

(2)使用 util 标签完成 list 集合注入提取 
<?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: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">
    <!--提取list集合类型属性的注入-->
    <util:list id="booklist">
        <value>易筋经</value>
        <value>九阴真经</value>
        <value>九阳神功</value>
    </util:list>

    <!--提取list集合类型属性的注入使用-->
    <bean id="book" class="com.atguigu.spring.collectiontype.Book">
        <property name="list" ref="booklist"></property>
    </bean>

</beans>
public class Book {
    private List<String> list;

    public void setList(List<String> list) {
        this.list = list;
    }

    public void test(){
        System.out.println(list);
    }
}
 @Test
    public void test1() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        Book book = context.getBean("book", Book.class);
        book.test();
    }

IOC 操作 Bean 管理(FactoryBean)

1、Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean(FactoryBean)

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("abc");
        return course;
    }

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

    @Override
    public boolean isSingleton() {
        return FactoryBean.super.isSingleton();
    }
}
<?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">
    <bean id="myBean" class="com.atguigu.spring.facbean.MyBean"></bean>
</beans>
 @Test
    public void test2() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean2.xml");
        Course course = context.getBean("myBean", Course.class);
        System.out.println(course);
    }

bean的作用域

1、在 Spring 里面,设置创建 bean 实例是单实例还是多实例

2、在 Spring 里面,默认情况下,bean 是单实例对象

在这里插入图片描述

3、如何设置单实例还是多实例

​ (1)在 spring 配置文件 bean 标签里面有属性(scope)用于设置单实例还是多实例

​ (2)scope 属性值

​ 第一个值 默认值,singleton,表示是单实例对象

​ 第二个值 prototype,表示是多实例对象
在这里插入图片描述
(3)singleton 和 prototype 区别

​ 第一 singleton 单实例,prototype 多实例

​ 第二
设置 scope 值是 singleton 时候,加载 spring 配置文件时候就会创建单实例对象设置 scope 值是prototype 时候,不是在加载 spring 配置文件时候创建 对象,在调用 getBean 方法时候创建多实 例对象

bean的生命周期

1、生命周期

​ (1)从对象创建到对象销毁的过程

2、bean 生命周期

(1)通过构造器创建 bean 实例(无参数构造) 

(2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法) 

(3)调用 bean 的初始化的方法(需要进行配置初始化的方法) 

(4)bean 可以使用了(对象获取到了) 

(5)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法) 

3、演示 bean 生命周期

public class Orders {

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

    private String oname;
    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 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">

    <bean id="orders" class="com.atguigu.spring.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="手机"></property>
    </bean>
  
</beans>
@Test
    public void test3() {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean3.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建的bean的实例对象");
        System.out.println(orders);

        //手动让bean实例销毁
        context.close();
    }

在这里插入图片描述

4、bean 的后置处理器,bean 生命周期有七步

​ (1)通过构造器创建 bean 实例(无参数构造)

​ (2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)

​ (3)把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization

​ (4)调用 bean 的初始化的方法(需要进行配置初始化的方法)

​ (5)把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization

​ (6)bean 可以使用了(对象获取到了)

​ (7)当容器关闭时候,调用 bean 的销毁的方法(需要进行配置销毁的方法)

5、演示添加后置处理器效果

​ (1)创建类,实现接口 BeanPostProcessor,创建后置处理器

<?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">

    <bean id="orders" class="com.atguigu.spring.bean.Orders" init-method="initMethod" destroy-method="destroyMethod">
        <property name="oname" value="手机"></property>
    </bean>
    
    <!--配置后置处理器-->
    <bean id="myBeanPost" class="com.atguigu.spring.bean.MyBeanPost"></bean>

</beans>
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;
    }

}
@Test
    public void test3() {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean3.xml");
        Orders orders = context.getBean("orders", Orders.class);
        System.out.println("第四步 获取创建的bean的实例对象");
        System.out.println(orders);

        //手动让bean实例销毁
        context.close();
    }

在这里插入图片描述

IOC 操作 Bean 管理(xml 自动装配)

1、什么是自动装配

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

2、演示自动装配过程

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

public class Dept {

    @Override
    public String toString() {
        return "Dept{}";
    }
}
public class Emp {
    private Dept dept;

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "dept=" + dept +
                '}';
    }

    public void Test() {
        System.out.println(dept);
    }
}

<?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">

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

    <bean id="dept" class="com.atguigu.spring.autowire.Dept"></bean>

</beans>
 @Test
    public void test4() {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("bean4.xml");
        Emp emp = context.getBean("emp", Emp.class);

        System.out.println(emp);

    }

IOC 操作 Bean 管理(外部属性文件

1、直接配置数据库信息

​ (1)配置德鲁伊连接池

​ (2)引入德鲁伊连接池依赖 jar 包

<?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">
    <!--直接配置连接池-->
    <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>

</beans>

2、引入外部属性文件配置数据库连接池

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

在这里插入图片描述

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

​ 引入 context 名称空间

<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名称空间-->

    <!--引入外部属性文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${prop.driverClass}"></property>
        <property name="url" value="${prop.url}"></property>
        <property name="username" value="${prop.userName}"></property>
        <property name="password" value="${prop.password}"></property>
    </bean>

</beans>

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

1、什么是注解

​ (1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)

​ (2)使用注解,注解作用在类上面,方法上面,属性上面

​ (3)使用注解目的:简化 xml 配置

2、Spring 针对 Bean 管理中创建对象提供注解

​ (1)@Component

​ (2)@Service

​ (3)@Controller

​ (4)@Repository

*上面四个注解功能是一样的,都可以用来创建 bean 实例

3、基于注解方式实现对象创建

​ 第一步 引入依赖

在这里插入图片描述

​ 第二步 开启组件扫描

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

<?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">
    <!--开启组件扫描
        1.如果扫描多个包,多个包直接逗号隔开  "com.atguigu.spring5.testdome,com.atguigu.spring5.dao"
        2.扫描包上层目录com.atguigu
    -->
    <context:component-scan base-package="com.atguigu.spring5"></context:component-scan>

</beans>
public class Testdemo {

    @Test
    public void TestService() {
        ApplicationContext context
                = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService",UserService.class);
        System.out.println(userService);
        userService.add();
    }
}
//注解中value可以不写默认是类的名称首字母小写
@Component(value = "userService") //注解等同于XML配置文件:<bean id="userService" 											class=".."/>
public class UserService {
    public void add() {
        System.out.println("service add.......");
    }
}

4、开启组件扫描细节配置

	<!--示例 1
     use-default-filters="false" 表示现在不使用默认 filter,自己配置 filter
     context:include-filter ,设置扫描哪些内容
    -->
    <context:component-scan base-package="com.atguigu" use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>								<!--代表只扫描Controller注解的类-->                    
    </context:component-scan>
        
    <!--示例 2
     下面配置扫描包所有内容
     context:exclude-filter: 设置哪些内容不进行扫描
    -->
    <context:component-scan base-package="com.atguigu">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>								<!--表示Controller注解的类之外一切都进行扫描-->
    </context:component-scan>

5、基于注解方式实现属性注入

​ (1)@Autowired:根据属性类型进行自动装配
第一步 把 service 和 dao 对象创建,在 service 和 dao 类添加创建对象注解
第二步 在 service 注入 dao 对象,在 service 类添加 dao 类型属性,在属性上面使用注解

@Service
public class UserService {

    //定义dao类型属性 不需要添加set方法 添加注入属性注解
    @Autowired  //根据类型进行注入
    private UserDao userDao;

    public void add() {
        System.out.println("service add.......");
        userDao.add();
    }
} 
public interface UserDao {
    public void add();
}
@Repository
public class UserDaoImpl implements UserDao{
    @Override
    public void add() {
        System.out.println("Dao add....");
    }
}
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao{
    @Override
    public void add() {
        System.out.println("Dao add....");
    }
}
<?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:component-scan base-package="com.atguigu.spring5"></context:component-scan>
</beans>   
public class Testdemo {

    @Test
    public void TestService() {
        ApplicationContext context
                = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService",UserService.class);
        System.out.println(userService);
        userService.add();
    }
}

(2)@Qualifier:根据名称进行注入 这个@Qualifier 注解的使用,和上面@Autowired 一起使用

@Service
public class UserService {

    //定义dao类型属性 不需要添加set方法 添加注入属性注解
    @Autowired  //根据类型进行注入
    @Qualifier(value = "userDaoImpl1")//根据名称进行注入 需要和@Autowired一起使用
    private UserDao userDao;

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

(3)@Resource:可以根据类型注入,可以根据名称注入

//import javax.annotation.Resource;
//@Resource//根据类型进行注入
    @Resource(name = "userDaoImpl1")//根据名称进行注入
    private UserDao userDao;

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

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

6、完全注解开发

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

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

}

(2)编写测试类

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

        UserService userService = context.getBean("userService",UserService.class);
        System.out.println(userService);
        userService.add();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值