• Spring新注解


    目录

     Spring新注解

    使用新注解替换xml文件

    web包下的UserController测试下

    运行结果


     Spring新注解

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

    非自定义的Bean的配置:<bean>

    加载properties文件的配置:<context:property-placeholder>

    组件扫描的配置:<context:component-scan>

    引入其他文件:<import>

    spring新注解

    使用新注解替换xml文件

    在config包下,创建名为DataSourceConfiguration类下

    1. package com.config;
    2. import com.mchange.v2.c3p0.ComboPooledDataSource;
    3. import org.springframework.beans.PropertyAccessException;
    4. import org.springframework.beans.factory.annotation.Value;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.PropertySource;
    7. import javax.sql.DataSource;
    8. import java.beans.PropertyVetoException;
    9. // 加载外部properties文件<context:property-placeholder location="classpath:MyJdbc.properties"/>
    10. @PropertySource("classpath:MyJdbc.properties")
    11. public class DataSourceConfiguration {
    12. @Value("${jdbc.driver}")
    13. private String driver;
    14. @Value("${jdbc.url}")
    15. private String url;
    16. @Value("${jdbc.password}")
    17. private String password;
    18. @Value("${jdbc.username}")
    19. private String username;
    20. @Bean("dataSource")//Spring会将当前方法的返回值以指定名称存储到Spring容器中
    21. public DataSource getDateSource() throws PropertyAccessException, PropertyVetoException {
    22. ComboPooledDataSource dataSource=new ComboPooledDataSource();
    23. dataSource.setDriverClass(driver);
    24. dataSource.setJdbcUrl(url);
    25. dataSource.setPassword(password);
    26. dataSource.setUser(username);
    27. return dataSource;
    28. }
    29. }

    在config包下创建SpringConfiguration类下

    1. package com.config;
    2. import com.mchange.v2.c3p0.ComboPooledDataSource;
    3. import org.springframework.beans.PropertyAccessException;
    4. import org.springframework.beans.factory.annotation.Value;
    5. import org.springframework.context.annotation.*;
    6. import javax.sql.DataSource;
    7. import java.beans.PropertyVetoException;
    8. //标志该类是Spring的核心配置类
    9. @Configuration
    10. // base是基本包他会扫描其子类下的所有包<context:component-scan base-package="com"/>
    11. @ComponentScan("com")
    12. //在总配置中加载分配置,加载核心配置类,若有多个,则写xx.class,xxx.class....
    13. @Import(DataSourceConfiguration.class)
    14. public class SpringConfiguration {
    15. }

    web包下的UserController测试下

    1. package com.web;
    2. import com.config.SpringConfiguration;
    3. import com.service.UserService;
    4. import org.springframework.context.ApplicationContext;
    5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    6. import org.springframework.context.support.ClassPathXmlApplicationContext;
    7. public class UserController {
    8. public static void main(String[] args) {
    9. //ApplicationContext app=new ClassPathXmlApplicationContext("ApplicationContext.xml");
    10. ApplicationContext app=new AnnotationConfigApplicationContext(SpringConfiguration.class);
    11. UserService userService = app.getBean(UserService.class);
    12. userService.save();
    13. }
    14. }

    运行结果

     

  • 相关阅读:
    JDBC001--java中的jdbc数据库的初步连接(MySQL8.0)
    短视频账号矩阵系统源码/技术源码分享/技术搭建架构
    Servlet
    Google Ads广告为Demand Gen推出生成式AI工具,可自动生成广告图片
    Java Object类方法简要解释(equals, hashCode, toString, finalize)
    使用vscode进行远程编辑和调试
    SQL语句-中级
    java基于springboot+vue的宠物领养饲养交流管理平台nodejs前后分离
    航空专场 | 无人机设计仿真流程讲解与案例实操
    微信小程序开发09 开放数据:微信生态帮助业务快速落地
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/125420102