SSM——Spring注解开发

文章目录

一. Spring原始注解

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

在这里插入图片描述

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

<!--注解的组件扫描,spring会扫描com.pudding这个包,及其子包-->
<context:component-scan base-package="com.pudding"></context:componentscan>

在这里插入图片描述

1. @Repository

//替换 <bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>

//@Component("userDao")
@Repository("userDao")  //功能和Component一样,但可读性强

//括号里面的字符串,代表bean中的id字段名

在这里插入图片描述

2. @Service

//<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">

//@Component("userService")
@Service("userService")

//括号里面的字符串,代表bean中的id字段名


//以下两句话替换bean的注入:<property name="userDao" ref="userDao"></property>
//@Autowired //只写Autowired,spring会按照数据类型从Spring容器中进行匹配的(只适合该类型有一个bean)

//@Qualifier("userDao")  //是按照id值(括号里面的值)从容器中进行匹配的 但是主要此处@Qualifier结合@Autowired一起使用,这样就是按照名称进行注入

@Resource(name="userDao") //@Resource相当于@Qualifier+@Autowired

注意:使用注解的方式,set方法可以省略,spring通过反射机制

在这里插入图片描述

3. @Value

在这里插入图片描述

4. @Scope

在这里插入图片描述

5. @PostConstruct、@PreDestroy

在这里插入图片描述

二. Spring新注解

使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:在这里插入图片描述
在这里插入图片描述

1. @Configuration、@ComponentScan、@Import

//标志该类是Spring的核心配置类
@Configuration

//替换:<context:component-scan base-package="com.itheima"/>
@ComponentScan("com.itheima")


@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;
   }


//替换:<import resource=""/>
@Import({DataSourceConfiguration.class}) //这地方是数组,通过逗号隔开
public class SpringCofiguration {

}

在这里插入图片描述

public static void main(String[] args) {

        //ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过注解进行加载配置文件
        ApplicationContext app = new AnnotationConfigApplicationContext(SpringCofiguration.class);
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }

2. @PropertySource、@Value

//替换:<context:property-placeholder location="classpath:jdbc.properties"/>
@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;
    }
}

在这里插入图片描述

3. @Bean

	@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;
    }

在这里插入图片描述
在这里插入图片描述

三. Spring集成Junit

1. 原始Junit测试Spring的问题

在这里插入图片描述

//ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过注解进行加载配置文件
ApplicationContext app = new AnnotationConfigApplicationContext(SpringCofiguration.class);
UserService userService = app.getBean(UserService.class);
userService.save();

上述问题解决思路

  • 让SpringJunit负责创建Spring容器,但是需要将配置文件的名称告诉它
  • 将需要进行测试Bean直接在测试类中进行注入

2. Spring集成Junit步骤

在这里插入图片描述

2.1 导入spring集成Junit的坐标

<!--此处需要注意的是,spring5 及以上版本要求 junit 的版本必须是 4.12 及以上-->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency

2.2 使用@Runwith注解替换原来的运行期

在这里插入图片描述

在这里插入图片描述

package com.itheima.test;

import com.itheima.cofig.SpringCofiguration;
import com.itheima.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.SQLException;

@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:applicationContext.xml") 加载配置文件
@ContextConfiguration(classes = {SpringCofiguration.class})
public class SpringJunitTest {

    @Autowired
    private UserService userService;

    @Autowired
    private DataSource dataSource;

    @Test
    public void test1() throws SQLException {
        userService.save();
        System.out.println(dataSource.getConnection());
    }

}

2.3 使用@ContextConfiguration指定配置文件或配置类

在这里插入图片描述

2.4 使用@Autowired注入需要测试的对象

在这里插入图片描述

2.5 创建测试方法进行测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值