基於註解的IOC案例-編寫spring的Ioc配置
阿新 • • 發佈:2021-01-14
技術標籤:spring
-
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
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();
}
-
domain層
public class Account implements Serializable { private Integer id; private String name; private Float money; public Integer getId() { return id; } public void setId
-
資料庫連線資訊jdbcConfig.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/nba jdbc.username=root jdbc.password=root
-
資料庫配置檔案
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; } }
-
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 { }
-
測試層
@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); } }