Spring新注解配置入门

原始注解:

<!--com.alibaba.druid.pool.DruidDataSource是对应DruidDataSource这个类-->
    <bean id="druiddataSource1" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>


    <import resource="applicationContext-user.xml"/>

    <context:component-scan base-package="cn.itcast"></context:component-scan>

上面就有很多东西,我们不能用原始注解替换,所以有了新注解:
在这里插入图片描述
1.首先我们创建一个SpringCofiguration(可改名)类(用来替换applicationContext.xml的)(替换xml,替换扫描、替换import要写在xml的情况)

package cn.itcast.cofig;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/12 13:26
 */
//标志该类是Spring的核心配置类
@Configuration

//<context:component-scan base-package="com.itheima"/>,代替applicationContext.xml的扫描
@ComponentScan("cn.itcast") //因为我的java类就有cn.itcast包,要看你自己创的

//<import resource=""/>,代替代替applicationContext.ml的import
@Import({DataSourceConfiguration.class}) //DataSourceConfiguration是自己创的子配置类,还可以在{}中加','添加多个子配置类

public class SpringCofiguration {
}

2.子配置类DataSourceConfiguration(替换加载pro文件、替换非自定义类要写在xml的情况):

package cn.itcast.cofig;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/12 13:31
 */

//<context:property-placeholder location="classpath:jdbc.properties"/>
@PropertySource("classpath:jdbc.properties")  //来替换applicationContext.xml的加载pro文件的
public class DataSourceConfiguration {


    /*以下是替换非自定义的对象
    <bean id="druiddataSource1" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>*/

    //右边为jdbc.pro左边一列,通过el表达式获取值再给value将它再付给下面的private string xxx;
    @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("dataSource1") //Spring会将当前方法的返回值以指定名称(dataSource1)存储到Spring容器中
    public DataSource getDataSource(){ //DataSource是个大接口所以可以接受DruidDataSource,所以返回为DataSource没事的
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
        
    }
}

当然你把DataSourceConfiguration的内容写在SpringCofiguration 也没事,但是我这里是为了掩饰SpringCofiguration 的@import才创建DataSourceConfiguration的

这个时候就可以把applicationContext.xml删除了
UserDao等参考上一篇博客:https://blog.csdn.net/GLOAL_COOK/article/details/112502132
3.测试:

package com.itcast;

import cn.itcast.cofig.SpringCofiguration;
import cn.itcast.service.UserService;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLException;

/**
 * @author QLBF
 * @version 1.0
 * @date 2021/1/11 20:38
 */
public class Test1 {
   
    @Test
    //测试Spring新注解
    public void test3() {
        //ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //要使用AnnotationConfigApplicationContext了:
        ApplicationContext app=new AnnotationConfigApplicationContext(SpringCofiguration.class);
        UserService userService = app.getBean(UserService.class);//用获取id的形式也可以,要强制转换一下而已
        userService.save();
    }
}

在这里插入图片描述
执行成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值