Spring - Spring注解开发详解、总结、案例

文章目录

  • Spring注解开发
  • Spring原始注解
  • 完善测试环境
    • 编写接口
      • 编写实现类
    • 编写service接口
      • 编写实现类
    • 编写spring.xml
    • 模拟web层
      • 编写测试类
    • 测试运行
  • 原始注解入门操作
    • 组件注解、Bean创建
      • UserDaoImpl.java
      • UserServiceImpl.java
    • 配置组件扫描
      • 编写spring.xml
    • 测试运行
  • 原始注解详解1
      • Repository注解代替
      • Service注解代替
        • 测试运行
      • 删除service层的set方法
        • 测试运行
      • Resource注解代替Autowired、Qualifier
        • 测试运行
  • 原始注解详解2
    • @Value
      • 测试运行
    • @Value.2
      • 测试运行
    • @Scope("prototype")
    • @PostConstruct,@PreDestroy
      • 编写下测试类,调用close方法
    • 测试运行
  • Spring新注解
    • 新注解
  • Spring新注解详解
    • 抛弃Spring.xml的配置
      • 创建类
      • 创建类2
      • 编写测试类
      • 测试运行

Spring注解开发

Spring原始注解

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

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

Spring - Spring注解开发详解、总结、案例_第1张图片

完善测试环境

编写接口

package com.taotao.dao;

/**
 * create by 刘鸿涛
 * 2022/4/11 19:53
 */
public interface UserDao {
    public void save();
}

编写实现类

package com.taotao.dao.impl;

import com.taotao.dao.UserDao;

/**
 * create by 刘鸿涛
 * 2022/4/11 19:55
 */

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

编写service接口

package com.taotao.service;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:02
 */
@SuppressWarnings({"all"})
public interface UserService {
    public void save();
}

编写实现类

package com.taotao.service.impl;

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

/**
 * create by 刘鸿涛
 * 2022/4/11 20:04
 */
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
    private UserDao userDao;

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

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

编写spring.xml


<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:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}">property>
        <property name="jdbcUrl" value="${jdbc.url}">property>
        <property name="user" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>

    bean>

    <bean id="userDao" class=" com.taotao.dao.impl.UserDaoImpl">bean>

    <bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao">property>
    bean>
beans>

模拟web层

编写测试类

package com.taotao.web;

import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:20
 */
@SuppressWarnings({"all"})
public class UserController {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

测试运行

Spring - Spring注解开发详解、总结、案例_第2张图片

原始注解入门操作

组件注解、Bean创建

UserDaoImpl.java

package com.taotao.dao.impl;

import com.taotao.dao.UserDao;
import org.springframework.stereotype.Component;

/**
 * create by 刘鸿涛
 * 2022/4/11 19:55
 */

//
@Component("userDao")       //userDao相当于id
@SuppressWarnings({"all"})
public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("save running...");
    }
}

UserServiceImpl.java

package com.taotao.service.impl;

import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:04
 */
//
@Component("userService")   //userService相当于id
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {

//  
    @Autowired
    @Qualifier("userDao")   //bean注入

    private UserDao userDao;

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

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

配置组件扫描

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


    <context:component-scan base-package="com.taotao">context:component-scan>

编写spring.xml


<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:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}">property>
        <property name="jdbcUrl" value="${jdbc.url}">property>
        <property name="user" value="${jdbc.username}">property>
        <property name="password" value="${jdbc.password}">property>

    bean>


    <context:component-scan base-package="com.taotao">context:component-scan>

beans>

测试运行

Spring - Spring注解开发详解、总结、案例_第3张图片

原始注解详解1

Repository注解代替

Spring - Spring注解开发详解、总结、案例_第4张图片

Service注解代替

Spring - Spring注解开发详解、总结、案例_第5张图片

测试运行

Spring - Spring注解开发详解、总结、案例_第6张图片

删除service层的set方法

package com.taotao.service.impl;

import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:04
 */
//
//@Component("userService")   //userService相当于id
    @Service("userService")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {

//  
    @Autowired
    @Qualifier("userDao")   //bean注入

    private UserDao userDao;
    
    @Override
    public void save() {
        userDao.save();
    }
}

测试运行

Spring - Spring注解开发详解、总结、案例_第7张图片

Resource注解代替Autowired、Qualifier

package com.taotao.service.impl;

import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:04
 */
//
//@Component("userService")   //userService相当于id
    @Service("userService")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {

//  
//    @Autowired              //按照数据类型从Spring容器中进行匹配的
//    @Qualifier("userDao")   //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
    @Resource(name="userDao")   //@Resource相当于@Autowired + @Qualifier
    private UserDao userDao;

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

测试运行

Spring - Spring注解开发详解、总结、案例_第8张图片

原始注解详解2

@Value

package com.taotao.service.impl;

import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:04
 */
//
//@Component("userService")   //userService相当于id
    @Service("userService")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
        @Value("itcast")
    private String driver;
//  
//    @Autowired              //按照数据类型从Spring容器中进行匹配的
//    @Qualifier("userDao")   //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
    @Resource(name="userDao")   //@Resource相当于@Autowired + @Qualifier
    private UserDao userDao;

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

测试运行

Spring - Spring注解开发详解、总结、案例_第9张图片

@Value.2

package com.taotao.service.impl;

import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:04
 */
//
//@Component("userService")   //userService相当于id
    @Service("userService")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
        @Value("${jdbc.driver}")
    private String driver;
//  
//    @Autowired              //按照数据类型从Spring容器中进行匹配的
//    @Qualifier("userDao")   //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
    @Resource(name="userDao")   //@Resource相当于@Autowired + @Qualifier
    private UserDao userDao;

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

测试运行

Spring - Spring注解开发详解、总结、案例_第10张图片

@Scope(“prototype”)

package com.taotao.service.impl;

import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:04
 */
//
//@Component("userService")   //userService相当于id
    @Service("userService")
    @Scope("prototype")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
        @Value("${jdbc.driver}")
    private String driver;
//  
//    @Autowired              //按照数据类型从Spring容器中进行匹配的
//    @Qualifier("userDao")   //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
    @Resource(name="userDao")   //@Resource相当于@Autowired + @Qualifier
    private UserDao userDao;

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

@PostConstruct,@PreDestroy

初始化后执行方法,和销毁后执行方法

package com.taotao.service.impl;

import com.taotao.dao.UserDao;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

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

/**
 * create by 刘鸿涛
 * 2022/4/11 20:04
 */
//
//@Component("userService")   //userService相当于id
    @Service("userService")
    @Scope("singleton")
@SuppressWarnings({"all"})
public class UserServiceImpl implements UserService {
        @Value("${jdbc.driver}")
    private String driver;
//  
//    @Autowired              //按照数据类型从Spring容器中进行匹配的
//    @Qualifier("userDao")   //是按照id值从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用
    @Resource(name="userDao")   //@Resource相当于@Autowired + @Qualifier
    private UserDao userDao;

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

    @PostConstruct
    public void init(){
        System.out.println("初始化...");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("已销毁...");
    }
}

编写下测试类,调用close方法

package com.taotao.web;

import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:20
 */
@SuppressWarnings({"all"})
public class UserController {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = app.getBean(UserService.class);
        userService.save();
        app.close();
    }
}

测试运行

Spring - Spring注解开发详解、总结、案例_第11张图片

Spring新注解

使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:

  • 非自定义的Bean的配置:
  • 加载properties文件的配置:
  • 组件扫描的配置:
  • 引入其他文件:

新注解

Spring - Spring注解开发详解、总结、案例_第12张图片

Spring新注解详解

抛弃Spring.xml的配置

创建类

package com.taotao.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * create by 刘鸿涛
 * 2022/4/11 22:22
 */
@SuppressWarnings({"all"})
//标志该类是Spring的核心配置类
@Configuration("com.taotao")    //加载包
//配置组件扫描
//
@ComponentScan("com.taotao")
//
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {


}

创建类2

package com.taotao.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * create by 刘鸿涛
 * 2022/4/11 22:37
 */
@SuppressWarnings({"all"})
@PropertySource("classpath:jdbc.properties")
//

public class DataSourceConfiguration {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource") //Spring会将当前方法的返回值以指定名称存储到Spring容器中
    public DataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

编写测试类

package com.taotao.web;

import com.taotao.config.SpringConfiguration;
import com.taotao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * create by 刘鸿涛
 * 2022/4/11 20:20
 */
@SuppressWarnings({"all"})
public class UserController {
    public static void main(String[] args) {
//        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

测试运行

Spring - Spring注解开发详解、总结、案例_第13张图片

你可能感兴趣的:(笔记,成长阶段,java,spring,maven)