Spring笔记(五) 新注解的开发

        对于原始的注解方式,只能针对于个人写的文件进行开发。但是如果一但引入到第三方的jar包的方式,再用原始的注解就会发现非常麻烦,需要我们到源文件中寻找加入注解,这样十分不利于开发的效率。针对这样的方式,就引入的新注解的方式。

 (此图转用黑马程序员的上课文件)

 @Configuration 一般就是单独写一个文件,在类的上面标注就行。

@ComponentScan 就是代替.xml文件进行对包的描扫注解

package com.fxy.config;

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

@Configuration
@ComponentScan("com.fxy")
public class SpringConfiguration {
    
}

@Bean跟@propertySourcety与@Bean

@Bean会把当前方法的返回值以指定的名称返回到spring容器中

package com.fxy.config;

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

import javax.sql.DataSource;

@Configuration
//<context:component-scan base-package="com.fxy"/>
@ComponentScan("com.fxy")
//<context:property-placeholder location="classpath:jdbc.properties"/>
@PropertySource("classpath:jdbc.properties")
public class SpringConfiguration {

    @Value("${jdbc.DriverClassName}")
    private String DriverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public DataSource getDateSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(DriverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }
}
//jdbc.properties文件
jdbc.DriverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/shangcheng
jdbc.username=root
jdbc.password=fxy

@import 这个注解是在一般是用在有很多个配置类中,将很多的分类导入总类中。

总类:

package com.fxy.config;

import org.springframework.context.annotation.*;


@Configuration
//<context:component-scan base-package="com.fxy"/>
@ComponentScan("com.fxy")
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {

    
}

分类:

package com.fxy.config;

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;

//<context:property-placeholder location="classpath:jdbc.properties"/>
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfiguration {
    @Value("${jdbc.DriverClassName}")
    private String DriverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    public DataSource getDateSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(DriverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }
}

最后在测试的时候也有需要要改动的,将原来的 ClassPathXmlApplicationContext()改为下面的方式:

import com.fxy.Service.UserService;
import com.fxy.config.SpringConfiguration;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class test1 {
    @Test
    public void test(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.ServiceSave();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值