Spring Configuration注解使用


@Configuration是spring.xml的注解版。

@ComponentScan是<context:component-scan base-package="com.coshaho.*" />標簽的注解版。

@ImportResource @Import是<import resource>標簽的注解版。

@PropertySource是<context:property-placeholder location="classpath:jdbc.properties"/>標簽的注解版。

@Bean是<bean>標簽的注解版。

@EnableTransactionManagement是tx:annotation-driven標簽的注解版。

 

我們把如下spring配置文件修改為@Configuration配置類

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!-- 注解注入 -->
    <context:component-scan base-package="com.coshaho.*" />
    
    <!-- 引入jdbc配置文件 -->   
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 使用spring初始化DataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 使用JdbcTemplate封裝DataSource -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 定義事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--使用注釋事務 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

對應配置類為

package com.coshaho;

import java.beans.PropertyVetoException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.coshaho.dao.UserDao;
import com.coshaho.service.UserService;
import com.coshaho.service.UserServiceImpl;
import com.mchange.v2.c3p0.ComboPooledDataSource;

@Configuration
@ComponentScan
@PropertySource("classpath:jdbc.properties")
//@Import(OtherConfiguration.class)
@EnableTransactionManagement 
//@ImportResource("classpath:transaction.xml")
public class ApplicationConfig 
{
    @Value("${jdbc.driverClass}")
    private String driverClass;
    
    @Value("${jdbc.jdbcUrl}")
    private String jdbcUrl;
    
    @Value("${jdbc.user}")
    private String user;
    
    @Value("${jdbc.password}")
    private String password;
    
    @Bean
    public ComboPooledDataSource dataSource() throws PropertyVetoException
    {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driverClass);
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setUser(user);
        dataSource.setPassword(password);
        return dataSource;
    }
    
    @Bean
    public JdbcTemplate jdbcTemplate() throws PropertyVetoException
    {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
    }
    
//    @Bean
//    public DataSourceTransactionManager transactionManager() throws PropertyVetoException
//    {
//        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
//        transactionManager.setDataSource(dataSource());
//        return transactionManager;
//    }
    
    @Bean
    public UserService userService(UserDao userDao)
    {
        UserServiceImpl userService = new UserServiceImpl();
        userService.setUserDao(userDao);
        return userService;
    }
    
    private static ApplicationContext context;
    public static void main(String[] args)
    {
        context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        UserService userService = (UserService) context.getBean("userService");
        System.out.println(userService.query(1));
        userService.create2User();
    }
}

詳細解釋一下

1、@ComponentScan不添加base-package值,則表示默認從配置類路徑掃描。

2、UserService依賴UserDao,則可以直接在構造方法中添加UserDao參數,spring自動按照類型注入。

3、@Import可以加載其他配置類。

4、一開始不知道如何在配置類中開啟事務,便把事務配置在一個單獨的xml中,通過@ImportResource引入。實際上直接使用@EnableTransactionManagement即可開啟事務。

 

vo層,dao層代碼和《基於注解的Spring事務配置》中的代碼一致,用例中新增ApplicationConfig和service層代碼,service層代碼如下

package com.coshaho.service;

import com.coshaho.vo.User;

public interface UserService 
{
    public void create(String name, int age);
    public User query(int id);
    public void create2User();
}
package com.coshaho.service;

import com.coshaho.vo.User;
import com.coshaho.dao.UserDao;

public class UserServiceImpl implements UserService
{
    private UserDao UserDao;
    
    public void create(String name, int age)
    {
        UserDao.create(name, age);
    }
    public User query(int id)
    {
        return UserDao.query(id);
    }
    public void create2User()
    {
        UserDao.create2User();
    }
    public void setUserDao(UserDao userDao) {
        UserDao = userDao;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM