1. 程式人生 > 其它 >基於註解的IOC案例-編寫spring的Ioc配置

基於註解的IOC案例-編寫spring的Ioc配置

技術標籤:spring

  1. dao層

    @Repository("accountDao")
    public class AccountDaoImpl implements IAccountDao {
    	@Autowired
        private QueryRunner runner;
        public List<Account> findAllAccount() {
            try{
                return runner.query("select * from account",new BeanListHandler
    <Account>(Account.class)); }catch (Exception e) { throw new RuntimeException(e); } } }
    public interface IAccountDao {
        List<Account> findAllAccount();
    

}


2. service層

```java
@Service("accountService")
public class AccountServiceImpl implements IAccountService{
    @Autowired
    private IAccountDao accountDao;
    public List<Account> findAllAccount() {
        List<Account> account= accountDao.findAllAccount();
        return accountDao.findAllAccount();
    }
}
   public interface IAccountService {
       List<Account> findAllAccount();
   }
   
  1. domain層

    public class Account implements Serializable {
    
        private Integer id;
        private String name;
        private Float money;
    
        public Integer getId() {
            return id;
        }
    
        public void setId
    (Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Float getMoney() { return money; } public void setMoney(Float money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
  2. 資料庫連線資訊jdbcConfig.properties

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/nba
    jdbc.username=root
    jdbc.password=root
    
  3. 資料庫配置檔案

    package com.chen.config;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.apache.commons.dbutils.QueryRunner;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.annotation.Scope;
    
    import javax.sql.DataSource;
    import java.beans.PropertyVetoException;
    
    public class DataSourceConfiguration {
        /**
      * <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(name="runner")
        @Scope("prototype")
        public QueryRunner createQueryRunner(DataSource dataSource){
            return new QueryRunner(dataSource);
        }
    
        @Bean(name = "dataSource")
        public DataSource CreateDs(){
            ComboPooledDataSource ds = new ComboPooledDataSource();
            try {
                ds.setDriverClass(driver);
                ds.setJdbcUrl(url);
                ds.setUser(username);
                ds.setPassword(password);
            } catch (PropertyVetoException e) {
                e.printStackTrace();
            }
            return ds;
        }
    }
    
  4. xml替代配置檔案

    //表示是spring的配置檔案
    @Configuration
    //<context:component-scan base-package="com.chen"></context:component-scan>
    @ComponentScan("com.chen")
    //<context:property-placeholder location="classpath:jdbcConfig.properties"></context:property-placeholder>
    @PropertySource("classpath:jdbcConfig.properties")
    /*@Import(JdbcConfig.class)*/
    @Import(DataSourceConfiguration.class)
    public class SpringConfiguration {
    }
    
  5. 測試層

    @RunWith(SpringJUnit4ClassRunner.class)
    /*@ContextConfiguration("classpath:bean.xml")*/
    @ContextConfiguration(classes = SpringConfiguration.class)
    public class AccountTest {
    
        @Autowired
        private IAccountService is;
    
        @Test
        public void test(){
            List<Account> account = is.findAllAccount();
            System.out.println(account);
        }
    }