基于注解的IOC案例-编写spring的Ioc配置

  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 findAllAccount() {
        List 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 {
           
        /**
      * 
         * 
         * 
         * 
         * 使用注解 将从容器中 注入属性
         */
        @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
    //
    @ComponentScan("com.chen")
    //
    @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);
        }
    }
    
    

你可能感兴趣的:(spring)