spring注解(Component、依赖注入、生命周期、作用域)

摘要:
˃测试类:publicclassTestCycle{publicstaticcvoidmain{ApplicationContextapplicationContext=newClassPathXmlApplicationContext;//创建容器对象StudentServicestudentService=applicationContext.getBean;studentService.addStudent();}在web开发中,提供了@Component注释的三个派生注释。它们是spring框架提供的三层中使用的注释,使我们的三层对象更加清晰。@存储库:dao层(持久层)@服务:服务层(业务层)@控制器:web层(表示层)@组件注释函数:用于将当前类的对象存储到spring容器中。属性:value:指定bean的ID。如果未写入,默认值为当前类名,首字母为小写。

1、注解

注解就是一个类,使用@加上注解名称,开发中可以使用注解取代配置文件

2、@Component 取代<bean  class="">,@Component 取代<bean id="" class="">

(1)创建一个类(该类与dao层无联系,是一个单独的类)

@Component("studentService")
public class StudentServiceImpl implements StudentService {
    public void addStudent(){
        System.out.println("StudentService的实现类的Add方法!!");
    }
}

(2)创建配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="pers.zhb.service"></context:component-scan>
</beans>

(3)测试类:

public class TestCycle {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
        StudentService studentService= (StudentService) applicationContext.getBean("studentService");
        studentService.addStudent();
    }
}

(4)web开发中,提供了3个@Component注解的衍生注解(功能一样),它们是spring框架为我们提供的三层使用的注解,使得我们的三层对象更加清晰

@Repository:dao层(持久层)

@Service:service层(业务层)

@Controller:web层(表现层)

 (5)@Component注解

作用:用于把当前类的对象存入到spring容器中

属性:

value:指定bean的id,不写的时候默认是当前的类名,且首字母小写。当指定属性的值的时候就要用该值

3、依赖注入

(1)创建一个Action:

@Controller("studentAction")
public class StudentAction {
    @Autowired//默认按照类型注入
    private StudentService studentService;
    public void execute(){
        studentService.addStudent();
    }
}

使用@Autowired注解(依赖注入)的时候可以写在属性或set方法处,写在属性处可以节省代码量,优先按照bean的类型去找。 

(2)service层:

public interface StudentService {
    public void addStudent();
}
@Service
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;

    @Qualifier("studentDao")
    public StudentDao getStudentDao() {
        return studentDao;
    }
    @Autowired
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    public void addStudent(){
        studentDao.addStudent();
    }
}

(3)dao层:

public interface StudentDao {
    public void addStudent();
}
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {
    @Override
    public void addStudent() {
        System.out.println("StudentDao的实现类的Add方法!!");
    }
}

(4)配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描-->
    <context:component-scan base-package="pers.zhb"></context:component-scan>
</beans>

(5)测试:

public class ActionTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new
                ClassPathXmlApplicationContext("applicationContext.xml");//创建容器对象
        StudentAction studentAction= (StudentAction) applicationContext.getBean("studentAction");
        studentAction.execute();
    }
}
StudentDao的实现类的Add方法!!

spring注解(Component、依赖注入、生命周期、作用域)第1张

 @Autowired

  • 可以省略类内的get和set方法以及配置文件中bean的依赖注入的内容,但是在spring容器中还是需要配置bean的,可以结合生成bean的注解使用
  • 自动按照类型注入,只要容器中有唯一一个bean对象类型和要注入的变量类型匹配,就可以注入成功
  • 出现的位置可以是变量上也可以是方法上
  • 按照类型注入的时候,如果有多个可以注入,不能注入成功的时候,可以按照名称注入(需要手动修改注入的变量名称),例如,一个接口有多个实现类,那么这些实现类就是相同类型的,注入的时候会出现问题,因为注入的时候匹配到了多个bean

spring注解(Component、依赖注入、生命周期、作用域)第2张

@Qualifier 

属性value用于指定注入的bean的id

在给类成员注入的时候不能独立使用,也就是说要和Autowired注解配合使用

@Resource

直接按照bean的id注入,可以独立使用

属性可以指定bean的id

以上三种注解都是能用于注入bean类型的数据,而基本类型和String类型无法使用上述注解

@Value:用于注入基本类型和String类型,属性用于指定值

4、生命周期

初始化:@PostConstruct

销毁:@PreDestory

分别添加到初始化和销毁方法之前。

5、作用域

多例:@Scope("prototype")

免责声明:文章转载自《spring注解(Component、依赖注入、生命周期、作用域)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇服务激活工具 ActivatorUtilities使用ssh远程执行命令批量导出数据库到本地(转)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

利用LDAP操作AD域

LDAP操作代码样例  初始化LDAP 目录服务上下文 该例子中,我们使用uid=linly,ou=People,dc=jsoso,dc=net这个账号,链接位于本机8389端口的LDAP服务器(ldap://localhost:8389),认证方式采用simple类型,即用户名/密码方式。 private static void initialConte...

Spring AOP之使用注解创建切面

上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象。那么现在就让我们使用AspectJ注解来定义切面吧。 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audience: package com.spring.aop.service.aop; import org.aspectj.lang.ProceedingJoi...

Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope

以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action、Spring MVC中的Controller) @Repository用于标注数据访问组件,即DAO组件 @Component泛指组件,当组件不好归类的时候,我们...

歌词的获取

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_...

MultipartFile(文件的上传)--CommonsMultipartResolver

一 : applicationContext.xml中:必须声明不然获取不到 <!-- 上传文件的配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">...

Spring Boot集成MyBatis的2种方式

目录 写在前面 准备工作 配置数据库驱动 配置数据源 原生集成MyBatis 依赖配置 注册MyBatis核心组件 定义并使用映射器 通过MyBatis-Spring-Boot-Starter集成 默认配置 高级定制 总结与比较 写在前面 最近总是有同事和技术群的朋友提问在Spring Boot中使用MyBatis时遇到的问题,大多...