Spring-Bean管理

什么是Bean管理

1.Spring创建对象
2.Spring注入属性

Bean管理的两种方式

1)基于 xml 配置文件方式实现
2)基于注解方式实现

基于xml方式

创建对象(基于xml)


创建对象时,默认执行无参构造函数创建对象

注入属性(基于xml)

依赖注入(DI)

package com.fly.spring5.entity;

public class Person {

  private int age;

  private String name;

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

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

  public void hello() {
    System.out.println("hello world");
  }

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

在配置文件中配置属性注入

<?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为类的全类名 -->
    <!--ApplicationContent.xml-->
    <bean id="person" class="com.fly.spring5.entity.Person">
        <property name="age" value="12"/>
        <property name="name" value="张三"/>
    </bean>

</beans>

测试并执行test1

package com.fly.spring5.test;

import com.fly.spring5.entity.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class TestPerson {

  @Test
  public void testHello() {
    //在实际过程中不会这么写,这里是为了测试,如果配置文件不是在类路径下,使用FileSystemXmlApplicationContext()
    //1。加载配置文件
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    //2.获取配置文件中创建的对象,第一个参数的值要和bean的id一致
    Person person = context.getBean("person", Person.class);
    person.hello();
  }

  @Test
  public void test1() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    Person person = context.getBean("person", Person.class);
    System.out.println(person);
  }
}

使用有参数构造进行注入


在Person类中添加有参构造函数

public Person(int age, String name) {
    this.age = age;
    this.name = name;
  }

修改配置文件

<?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为类的全类名 -->
    <!--ApplicationContent.xml-->
    <bean id="person" class="com.fly.spring5.entity.Person">
        <constructor-arg name="age" value="12"/>
        <constructor-arg name="name" value="张三"/>
    </bean>

</beans>

测试

  @Test
  public void test1() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    Person person = context.getBean("person", Person.class);
    System.out.println(person);
  }

p命名空间

在配置文件中添加p命名空间

修改配置文件

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


    <!--id是自己起的别名,class为类的全类名 -->
    <!--ApplicationContent.xml-->
    <bean id="person" class="com.fly.spring5.entity.Person" p:age="12" p:name="张三">

    </bean>

</beans>

测试

  @Test
  public void test1() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    Person person = context.getBean("person", Person.class);
    System.out.println(person);
  }

注入特殊类型属性

1)null

修改配置文件

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


    <!--id是自己起的别名,class为类的全类名 -->
    <!--ApplicationContent.xml-->
    <bean id="person" class="com.fly.spring5.entity.Person" p:age="12">
        <property name="name">
            <null/>
        </property>
    </bean>

</beans>

测试

@Test
  public void test1() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    Person person = context.getBean("person", Person.class);
    System.out.println(person);
  }


2)值包含特殊字符
1 把特殊字符进行转义,例如&lt、&gt
2 使用CDATA
修改配置文件

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


    <!--id是自己起的别名,class为类的全类名 -->
    <!--ApplicationContent.xml-->
    <bean id="person" class="com.fly.spring5.entity.Person" p:age="12">
        <property name="name">
            <value><![CDATA[<<哈哈>>]]]]></value>
        </property>
    </bean>

</beans>

测试

 @Test
  public void test1() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    Person person = context.getBean("person", Person.class);
    System.out.println(person);
  }

注入外部bean

新建PersonDao接口和实现类、PersonService类

package com.fly.spring5.dao;

public interface PersonDao {
  void show();
}
package com.fly.spring5.dao.impl;

import com.fly.spring5.dao.PersonDao;

public class PersonDaoImpl implements PersonDao {
  @Override
  public void show() {
    System.out.println("======PersonDaoImpl show=====");
  }
}
package com.fly.spring5.service;

import com.fly.spring5.dao.PersonDao;

public class PersonService {

  public void setPersonDao(PersonDao personDao) {
    this.personDao = personDao;
  }

  private PersonDao personDao;

  public void show() {
    System.out.println("=====PersonService show======");
    personDao.show();
  }

}

配置文件

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


    <!--id是自己起的别名,class为类的全类名 -->
    <!--ApplicationContent.xml-->
    <bean id="person" class="com.fly.spring5.entity.Person" p:age="12">
        <property name="name">
            <value><![CDATA[<<哈哈>>]]]]></value>
        </property>
    </bean>

    <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/>

    <bean id="personService" class="com.fly.spring5.service.PersonService">
        <property name="personDao" ref="personDaoImpl"/>
    </bean>

</beans>

测试

 @Test
  public void test2() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    PersonService personService = context.getBean("personService", PersonService.class);
    personService.show();
  }

注入内部bean

新建Grade类和Student类

package com.fly.spring5.entity;

public class Grade {
  @Override
  public String toString() {
    return "Grade{" +
            "gradeName='" + gradeName + '\'' +
            '}';
  }

  public void setGradeName(String gradeName) {
    this.gradeName = gradeName;
  }

  private String gradeName;
}

package com.fly.spring5.entity;

public class Student {
  private int stuAge;
  private String stuName;
  private Grade grade;

  @Override
  public String toString() {
    return "Student{" +
            "stuAge=" + stuAge +
            ", stuName='" + stuName + '\'' +
            ", grade=" + grade +
            '}';
  }

  public void setStuAge(int stuAge) {
    this.stuAge = stuAge;
  }

  public void setStuName(String stuName) {
    this.stuName = stuName;
  }

  public void setGrade(Grade grade) {
    this.grade = grade;
  }
}

配置文件

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


    <!--id是自己起的别名,class为类的全类名 -->
    <!--ApplicationContent.xml-->
    <bean id="person" class="com.fly.spring5.entity.Person" p:age="12">
        <property name="name">
            <value><![CDATA[<<哈哈>>]]]]></value>
        </property>
    </bean>

    <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/>

    <bean id="personService" class="com.fly.spring5.service.PersonService">
        <property name="personDao" ref="personDaoImpl"/>
    </bean>



    <bean id="student" class="com.fly.spring5.entity.Student">
        <property name="stuAge" value="10"/>
        <property name="stuName" value="张三"/>
        <property name="grade">
            <bean class="com.fly.spring5.entity.Grade">
                <property name="gradeName" value="1年级"/>
            </bean>
        </property>
    </bean>

</beans>

测试

  @Test
  public void test3() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    Student student = context.getBean("student", Student.class);
    System.out.println(student);
  }

级联赋值

1

 <bean id="student" class="com.fly.spring5.entity.Student">
        <property name="stuAge" value="10"/>
        <property name="stuName" value="张三"/>
       <property name="grade" ref="grade"/>
    </bean>

    <bean id="grade" class="com.fly.spring5.entity.Grade">
        <property name="gradeName" value="1年级"/>
    </bean>

2

    <bean id="student" class="com.fly.spring5.entity.Student">
        <property name="stuAge" value="10"/>
        <property name="stuName" value="张三"/>
        <property name="grade" ref="grade"/>
        <property name="grade.gradeName" value="1年级"/>
    </bean>

    <bean id="grade" class="com.fly.spring5.entity.Grade">
    </bean>

这种方式要操作的对象要有get方法

注入集合类型属性

注入基本数据类型的集合属性

修改Student类

package com.fly.spring5.entity;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Student {
  private int stuAge;
  private String stuName;
  private Grade grade;

  private String[] hobbies;
  private List<String> list;
  private Map<String,String> map;

  public void setHobbies(String[] hobbies) {
    this.hobbies = hobbies;
  }

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

  public void setMap(Map<String, String> map) {
    this.map = map;
  }

  public Grade getGrade() {
    return grade;
  }


  @Override
  public String toString() {
    return "Student{" +
            "stuAge=" + stuAge +
            ", stuName='" + stuName + '\'' +
            ", grade=" + grade +
            ", hobbies=" + Arrays.toString(hobbies) +
            ", list=" + list +
            ", map=" + map +
            '}';
  }

  public void setStuAge(int stuAge) {
    this.stuAge = stuAge;
  }

  public void setStuName(String stuName) {
    this.stuName = stuName;
  }

  public void setGrade(Grade grade) {
    this.grade = grade;
  }
}

修改配置文件

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


    <!--id是自己起的别名,class为类的全类名 -->
    <!--ApplicationContent.xml-->
    <bean id="person" class="com.fly.spring5.entity.Person" p:age="12">
        <property name="name">
            <value><![CDATA[<<哈哈>>]]]]></value>
        </property>
    </bean>

    <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/>

    <bean id="personService" class="com.fly.spring5.service.PersonService">
        <property name="personDao" ref="personDaoImpl"/>
    </bean>



    <bean id="student" class="com.fly.spring5.entity.Student">
        <property name="stuAge" value="10"/>
        <property name="stuName" value="张三"/>
        <property name="grade" ref="grade"/>
        <property name="grade.gradeName" value="1年级"/>
        <property name="hobbies">
            <array>
                <value>arr1</value>
                <value>arr2</value>
                <value>arr3</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="k1" value="v1"/>
                <entry key="k2" value="v2"/>
                <entry key="k3" value="v3"/>
            </map>
        </property>
    </bean>

    <bean id="grade" class="com.fly.spring5.entity.Grade">
    </bean>

</beans>

测试

  @Test
  public void test3() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    Student student = context.getBean("student", Student.class);
    System.out.println(student);
  }

注入对象类型的集合属性

创建School类

package com.fly.spring5.entity;

import java.util.List;

public class School {

  private List<Grade> grades;

  @Override
  public String toString() {
    return "School{" +
            "grades=" + grades +
            '}';
  }

  public void setGrades(List<Grade> grades) {
    this.grades = grades;
  }
}

修改配置文件

<bean id="grade" class="com.fly.spring5.entity.Grade">
        <property name="gradeName" value="1年级"/>
    </bean>

    <bean id="grade2" class="com.fly.spring5.entity.Grade">
        <property name="gradeName" value="2年级"/>
    </bean>

    <bean id="school" class="com.fly.spring5.entity.School">
        <property name="grades">
            <list>
                <ref bean="grade"/>
                <ref bean="grade2"/>
            </list>
        </property>
    </bean>

测试

  @Test
  public void test4() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    School school = context.getBean("school", School.class);
    System.out.println(school);
  }

提取集合注入部分

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

修改配置文件

<bean id="grade" class="com.fly.spring5.entity.Grade">
        <property name="gradeName" value="1年级"/>
    </bean>

    <bean id="grade2" class="com.fly.spring5.entity.Grade">
        <property name="gradeName" value="2年级"/>
    </bean>

    <util:list id="gradeList">
        <ref bean="grade"/>
        <ref bean="grade2"/>
    </util:list>

    <bean id="school" class="com.fly.spring5.entity.School">
        <property name="grades" ref="gradeList">
        </property>
    </bean>

测试

@Test
  public void test4() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    School school = context.getBean("school", School.class);
    System.out.println(school);
  }

FactoryBean

1)Spring 有两种类型 bean,一种普通 bean,另外一种工厂 bean
2)普通 bean:在配置文件中定义 bean 类型就是返回类型
3)工厂 bean:在配置文件定义 bean 类型可以和返回类型不一样
创建MyBean类

package com.fly.spring5.entity;

import org.springframework.beans.factory.FactoryBean;

public class MyBean implements FactoryBean<Grade> {
  @Override
  public Grade getObject() throws Exception {
    Grade grade = new Grade();
    grade.setGradeName("1年级");
    return grade;
  }

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

配置文件

 <bean id="myBean" class="com.fly.spring5.entity.MyBean"/>

测试

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

Bean作用域


在Spring中,默认情况下bean是单例
测试

 @Test
  public void test6() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    School school = context.getBean("school", School.class);
    School school2 = context.getBean("school", School.class);
    System.out.println(school == school2);
  }


如何设置单实例还是多实例?

修改bean标签的scope属性,默认值是singleton,prototype表示是多实例对象,修改配置文件后再次运行test6

<bean id="school" class="com.fly.spring5.entity.School" scope="prototype">
        <property name="grades" ref="gradeList">
        </property>
    </bean>

    <bean id="myBean" class="com.fly.spring5.entity.MyBean"/>

Bean生命周期



1)通过构造器创建 bean 实例(无参数构造)
2)为 bean 的属性设置值和对其他 bean 引用(调用 set 方法)
3)把 bean 实例传递 bean 后置处理器的方法 postProcessBeforeInitialization
4)调用 bean 的初始化的方法(需要进行配置初始化的方法)
5)把 bean 实例传递 bean 后置处理器的方法 postProcessAfterInitialization
6)bean 可以使用了(对象获取到了)
7)当容器关闭时候,调用 bean 的销毁的方法

xml 自动装配

1)根据属性名称注入 ,注入值 bean 的 id 值和类属性名称一样
修改配置文件

  <bean id="student" class="com.fly.spring5.entity.Student" autowire="byName">
        <property name="stuAge" value="10"/>
        <property name="stuName" value="张三"/>
        <property name="hobbies">
            <array>
                <value>arr1</value>
                <value>arr2</value>
                <value>arr3</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="k1" value="v1"/>
                <entry key="k2" value="v2"/>
                <entry key="k3" value="v3"/>
            </map>
        </property>
    </bean>


    <bean id="grade" class="com.fly.spring5.entity.Grade">
        <property name="gradeName" value="1年级"/>
    </bean>

测试

 @Test
  public void test3() {
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContent.xml");
    Student student = context.getBean("student", Student.class);
    System.out.println(student);
  }


2)根据属性类型注入,但是这个类型的Bean只能创建一个

外部属性文件

引入德鲁伊连接池所需jar包

创建文件存储数据库的信息

引入context命名空间

修改配置文件

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


    <!--id是自己起的别名,class为类的全类名 -->
    <!--ApplicationContent.xml-->
    <bean id="person" class="com.fly.spring5.entity.Person" p:age="12">
        <property name="name">
            <value><![CDATA[<<哈哈>>]]]]></value>
        </property>
    </bean>

    <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/>

    <bean id="personService" class="com.fly.spring5.service.PersonService">
        <property name="personDao" ref="personDaoImpl"/>
    </bean>



    <bean id="student" class="com.fly.spring5.entity.Student">
        <property name="stuAge" value="10"/>
        <property name="stuName" value="张三"/>
        <property name="grade" ref="grade"/>
        <property name="grade.gradeName" value="1年级"/>
        <property name="hobbies">
            <array>
                <value>arr1</value>
                <value>arr2</value>
                <value>arr3</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value>list1</value>
                <value>list2</value>
                <value>list3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="k1" value="v1"/>
                <entry key="k2" value="v2"/>
                <entry key="k3" value="v3"/>
            </map>
        </property>
    </bean>


    <bean id="grade" class="com.fly.spring5.entity.Grade">
        <property name="gradeName" value="1年级"/>
    </bean>

    <bean id="grade2" class="com.fly.spring5.entity.Grade">
        <property name="gradeName" value="2年级"/>
    </bean>

    <util:list id="gradeList">
        <ref bean="grade"/>
        <ref bean="grade2"/>
    </util:list>

    <bean id="school" class="com.fly.spring5.entity.School" scope="prototype">
        <property name="grades" ref="gradeList">
        </property>
    </bean>

    <bean id="myBean" class="com.fly.spring5.entity.MyBean"/>

    <!--引入外部属性文件--> <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--配置连接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${prop.driverClass}"/>
    <property name="url" value="${prop.url}"/>
    <property name="username" value="${prop.userName}"/>
    <property name="password" value="${prop.password}"/>
</bean>

</beans>

基于注解方式

所需的依赖

在配置文件中开启注解扫描,这个非常重要!!!

<context:component-scan base-package="com.fly.spring5"/>

@Component

修改PersonDaoImpl类

package com.fly.spring5.dao.impl;

import com.fly.spring5.dao.PersonDao;
import org.springframework.stereotype.Component;

@Component
/**
 * @Component:组件、组成部分,表示将自己交给Spring托管,相当于下面这句代码
 * <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/>
 * 如果没有起名字,默认是类名的首字母小写,例如PersonDaoImpl==>personDaoImpl,Student ==>student
 * 如果想要自定义名称:@Component("personDaoImpl")
 */
public class PersonDaoImpl implements PersonDao {
  @Override
  public void show() {
    System.out.println("======PersonDaoImpl show=====");
  }
}

运行测试方法showTest

  @Test
  public void showTest() {
    ApplicationContext context =
            new ClassPathXmlApplicationContext("ApplicationContent.xml");
    PersonDao personDaoImpl = context.getBean("personDaoImpl", PersonDao.class);
    personDaoImpl.show();
  }

@Autowired

之前通过配置文件service调用dao的方法

package com.fly.spring5.service;

import com.fly.spring5.dao.PersonDao;


public class PersonService {

  public void setPersonDao(PersonDao personDao) {
    this.personDao = personDao;
  }


  private PersonDao personDao;

  public void show() {
    System.out.println("=====PersonService show======");
    personDao.show();
  }

}

配置文件

 <bean id="personDaoImpl" class="com.fly.spring5.dao.impl.PersonDaoImpl"/>
  <bean id="personService" class="com.fly.spring5.service.PersonService">
      <property name="personDao" ref="personDaoImpl"/>
  </bean>

测试方法

  @Test
  public void test1() {
    ApplicationContext context =
            new ClassPathXmlApplicationContext("ApplicationContent.xml");
    PersonService personService = context.getBean("personService", PersonService.class);
    personService.show();
  }


使用注解方式实现

package com.fly.spring5.service;

import com.fly.spring5.dao.PersonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class PersonService {

  private final PersonDao personDao;

  @Autowired
  public PersonService(PersonDao personDao) {
    this.personDao = personDao;
  }

  public void show() {
    System.out.println("=====PersonService show======");
    personDao.show();
  }

}

还是刚才的测试方法

注意:1)根据类型来注入,如果有多个相同的类型,会报错
2)如果根据名字来指定注入,使用注解指定 @Qualifier("personDaoImpl")
3)注解也可以加在私有属性上面,Spring通过反射技术直接注入,不会调用set方法
4)也可以加在构造函数上面

@Resource

@Resource注解和@Autowired功能基本上一样,@Autowired是Spring提供的,@Resource是Java官方提供的注入注解,推荐使用
如果没有依赖,导入这个包

import javax.annotation.Resource;

Resource依然飘红,点击Resource会提示 Add Java EE 6 JARS to module dependencies,点击下载所需的包即可。

package com.fly.spring5.service;

import com.fly.spring5.dao.PersonDao;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;


@Component
public class PersonService {

  //指定名称:@Resource(name = "personDaoImpl")
  @Resource
  private  PersonDao personDao;
  

  public void show() {
    System.out.println("=====PersonService show======");
    personDao.show();
  }

}

测试方法

  @Test
  public void test1() {
    ApplicationContext context =
            new ClassPathXmlApplicationContext("ApplicationContent.xml");
    PersonService personService = context.getBean("personService", PersonService.class);
    personService.show();
  }

@Controller,@Repository,@Service和@Component

这四个注解作用一样,放不同的注解可读性更好
1)@Controller:控制器,通常放在controller层
2)@Repository:仓库、数据访问对象,通常放在dao层
3)@Service:业务逻辑层
4)@Component:组件

posted @ 2021-10-29 00:22  翻蹄亮掌一皮鞋  阅读(275)  评论(0)    收藏  举报