Spring 的纯注解配置

知道每个注解的意思:

|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 注解 | 说明 |
| @Configuration | 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解,作用等价于applicationContext.xml 配置文件。 |
| @ComponentScan | 用于指定 Spring在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中的 <context:component-scan base-package="com.sy"/>一样 |
| @Bean | 使用在方法上,标注将该方法的返回值存储到 Spring容器中。 id的值默认是方法的明名称,可以自定义id的值 |
| @PropertySource | 用于加载xxx.properties 文件中的配置结合 @Value("${}") 取配置文件的值。作用类似于:<context:property-placeholder location="classpath:db.properties"></context:property-placeholder> |
| @Import | 用于导入其他配置类 |

定义一个配置类:SpringConfig

java 复制代码
/*普通类变成配置类 需要在类上添加@Configuration*/

/*配置类注解*/ /*这个类是配置类*/
@Configuration
/*包扫描注解   <context:component-scan base-package="com.study"></context:component-scan>*/
@ComponentScan(basePackages = "com.study")
/*导入其他配置类*/
@Import(DataSourceConfig.class)
public class SpringConfig {
}

数据库连接信息配置类:DataSourceConfig

java 复制代码
/*
* 数据库连接信息配置类
* */
//加载配置文件
@PropertySource("classpath:db.properties")
public class DataSourceConfig {

    /*
    *   <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>*/
    @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
    public DataSource dataSource() throws Exception {
        /*创建数据库连接池*/
        ComboPooledDataSource source = new ComboPooledDataSource();
        /*把文件赋予变量的值  赋到连接池的属性中*/
        source.setDriverClass(driver);
        source.setJdbcUrl(url);
        source.setUser(username);
        source.setPassword(password);
        return source;
    }

    /*
    *           <!--配置jdbcTemplate模板对象-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <!--将数据源交给jdbcTemplate模板对象-->
            <property name="dataSource" ref="dataSource"></property>
        </bean>*/
    @Bean
    public JdbcTemplate jdbcTemplate(@Autowired @Qualifier("dataSource") DataSource dataSource){
        /*它是一个数据访问抽象层,为使用JDBC进行数据库操作提供了便利*/
        return new JdbcTemplate(dataSource);
    }
}

持久层:

java 复制代码
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /*
    *查询全部账户信息
    * */
    @Override
    public List<Account> findAllAccounts() {
        String sql="select * from account";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Account>(Account.class));
    }
}
业务层:
java 复制代码
@Service("accountService")
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    @Override
    public List<Account> findAllAccounts() {
        return accountDao.findAllAccounts();
    }
}

方法测试可以用spring 和junit整合:

java 复制代码
/*将 Spring 和 Junit 链接了起来*/
@RunWith(SpringJUnit4ClassRunner.class)
/*配置类*/
@ContextConfiguration(classes = SpringConfig.class)
public class AccountController {
    /*实例化对象*/
    @Autowired
    private AccountService accountService;

    @Test
    public void findAllAccountsTests(){
        List<Account> accounts = accountService.findAllAccounts();
        for (Account account : accounts) {
            System.out.println(account);
        }
    }
}
相关推荐
智购科技无人售货机工厂店10 分钟前
自动售货机刷脸支付集成实战:活体检测与合规门槛~YH
java·服务器·前端·科技·零售
小敬爱吃饭14 分钟前
PgHero轻量PostgreSQL监控:Docker部署、连接统计与执行计划
数据库·docker·postgresql
2401_8914092639 分钟前
期权全量合约历史数据:分钟/Tick/日级/L2五档字段清单
数据库
海宇服务1 小时前
零信任架构实战:基于海宇企业司法涉诉构建自动化分包商准入合规审查网关
数据库·人工智能·架构·自动化
Nazarick1 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
黑色的白兔No11 小时前
若依平台cloud版本搭建
java·spring cloud·微服务·nacos·vue·若依
W_son2 小时前
数据库归档模式开启与备份作业配置实战
数据库·oracle·达梦数据库
郝学胜-神的一滴2 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
syf0611132 小时前
【Java】synchronized 关键字详解:从字节码到对象头与锁升级
java·开发语言
其实防守也摸鱼2 小时前
Day 3 · Agent Loop:从调用模型到控制循环 —— 课件精讲笔记
服务器·开发语言·javascript·数据库·人工智能·笔记·ecmascript