Spring注解开发-原始注解

1.Spring原始注解

Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率。

任何一套框架,现在都是两套配置,一套是xml配置,一套是注解配置

Spring原始注解主要是替代<Bean>的配置

 (1)之前用xml配置的方式

 dao层:

UserDao

package com.company.dao;

public interface UserDao {
    public void save();
}

UserDaoImpl

package com.company.dao.impl;

import com.company.dao.UserDao;

public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("save running...");
    }

}

  Service层:

UserService

package com.company.service;

public interface UserService {
    public void save();
}

UserServiceImpl

package com.company.service.impl;

import com.company.dao.UserDao;
import com.company.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDao userDao;

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

    public void save(){
        userDao.save();
    }
}

  Controller层(web层)

package com.company.web;

import com.company.service.UserService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserController {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = applicationContext.getBean(UserService.class);
        userService.save();
    }
}

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

    <bean id="userDao" class="com.company.dao.impl.UserDaoImpl"></bean>
    <bean id="userService" class="com.company.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
</beans>

  运行结果:

 (2)现在通过注解的方式(原始注解):

使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法

UserDaoImpl中:

UserServiceImpl中:

(3)使用@Controller,@Service,@Repository替代@Component

 我们看到@Component时,不能意识到它属于哪一层, 使用@Controller,@Service,@Repository语义可以更加明确知道属于哪一层,它们三个的使用方式和@Component一样的

 注意:如果使用xml配置的方式set方法需要写的,如果使用注解的方式,set方法可以不写

 @Qualifier("userDao")去掉仍然可以,直接单独使用@Autowired就可以了,为什么呢?

因为@Autowired按照数据类型从Spring容器中进行匹配的

在userDao上面加了一个@Autowired,Spring扫到这个注解之后,会尝试从Spring容器找到UserDao类型的Bean,找到之后,直接给你注到这个地方

@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。在使用@Autowired之前,我们对一个bean配置起属性时,是这样用的

<property name="属性名" value=" 属性值"/>  

通过这种方式来,配置比较繁琐,而且代码比较多。在Spring 2.5 引入了 @Autowired 注释

 如果现在不想按照类型匹配,我想按照名称匹配

 这样可以用@Resource

 (4)注入普通数据类型,用@Value

运行结果:

用SpEL(spring表达式)可以获取Spring容器中那个key指定的值,key如果匹配成功,可以将值赋给下面的driver,即${jdbc.driver}

 运行结果:

 (5)@scope产生单例或者多例的Bean

 

 

 (6)使用@PostConstruct和@PreDestroy

package com.company.service.impl;

import com.company.dao.UserDao;
import com.company.service.UserService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

// <bean id="userService" class="com.company.service.impl.UserServiceImpl">
// @Component("userService")
@Service("userService")
@Scope("singleton")
// @Scope("prototype")
public class UserServiceImpl implements UserService {
    @Value("${jdbc.driver}")
    private String driver;
    @Resource(name="userDao")
    private UserDao userDao;

    public void save(){
        System.out.println(driver);
        userDao.save();
    }

    @PostConstruct  // 在构造器执行之后(创建对象之后),执行这个初始化方法
    public void init() {
        System.out.println("Service对象初始化方法。。。");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("Service对象销毁方法。。。");
    }
}

 运行结果:

原文地址:https://www.cnblogs.com/GumpYan/p/14116649.html