[사용 소감] 마벤2의 m2eclipse 사용 매뉴얼의 4가지 일반적인 Dao 실례 작성 (3)
13093 단어 m2eclipse
AbstractBasicService:spring 주석으로basicDao를 주입하고 사용하는 모든 서비스 클래스는AbstractBasicService를 계승하여 인용된bean의class를AbstractBasicService에 주입합니다
public abstract class AbstractBasicService<T extends JavaBean, PK extends Serializable>
implements BasicServiceIf<T, PK> {
private BasicDaoIf<T, PK> basicDao;
protected Class<T> javabeanclass;
/**
* AbstractBasicService.
*
* @param javabeanclass
*/
protected AbstractBasicService(Class<T> javabeanclass) {
this.javabeanclass = javabeanclass;
}
/**
* basicDao
*
* @return the basicDao
* @since CodingExample Ver( ) 1.0
*/
public BasicDaoIf<T, PK> getBasicDao() {
return basicDao;
}
/**
* @param basicDao
* the basicDao to set
*/
@Resource(name = "basicDao")
public void setBasicDao(BasicDaoIf<T, PK> basicDao) {
this.basicDao = basicDao;
}
@Override
public List<T> findAll() {
return basicDao.findAll(javabeanclass);
}
}
, , src/test/java , spring src/test/resources ,
spring applicationContext.xml :
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="org.lxh" />
<tx:annotation-driven transaction-manager="hibernateTransactionManager" />
</beans>
스프링 3.02 현재 자바 클래스를 bean 탭으로 사용할 수 있는 새로운 기능이 있습니다. 따라서 스프링 프로필은 두 마디만 쓰면 됩니다. 업무를 가지고 가려면 세 마디입니다. 스프링 코드는 다음과 같습니다.
@Configuration
public class BeanConfig {
private static PropertiesFactory factory = PropertiesFactory.getInstance();
@Bean(name = { "basicDao" })
public BasicDaoIf<JavaBean, Serializable> basicDao() {
return BasicDaoFactory.getBasicDao();
}
@Bean(name = { "dataSource" })
public BasicDataSource dataSource() {
BasicDataSource bds = new BasicDataSource();
bds.setDriverClassName((String) factory
.getPropertyValueOrProperties("driverClassName"));
bds.setUrl((String) factory.getPropertyValueOrProperties("url"));
bds.setUsername((String) factory
.getPropertyValueOrProperties("username"));
bds.setPassword((String) factory
.getPropertyValueOrProperties("password"));
return bds;
}
@Bean(name = { "sessionFactory" })
public SessionFactory getSessionFactory() throws Exception {
AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
sessionFactoryBean.setDataSource(this.dataSource());
sessionFactoryBean.setHibernateProperties((Properties) factory
.getPropertyValueOrProperties(""));
String packagesToScan = (String) factory
.getPropertyValueOrProperties("packages");
String[] packages = packagesToScan.split(":");
if (null == packages) {
sessionFactoryBean
.setPackagesToScan(new String[] { packagesToScan });
} else {
sessionFactoryBean.setPackagesToScan(packages);
}
sessionFactoryBean.afterPropertiesSet();
return sessionFactoryBean.getObject();
}
@Bean(name = { "hibernateTemplate" })
public HibernateTemplate getHibernateTemplate() throws Exception {
HibernateTemplate hibernateTemplate = new HibernateTemplate();
hibernateTemplate.setSessionFactory(this.getSessionFactory());
return hibernateTemplate;
}
@Bean(name = { "hibernateTransactionManager" })
public HibernateTransactionManager getHibernateTransactionManager()
throws Exception {
HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
hibernateTransactionManager.setSessionFactory(this.getSessionFactory());
return hibernateTransactionManager;
}
}
bean 중 하나는properties를 읽어야 하기 때문에 설정 파일을 읽는 공장에 추가해야 합니다
PropertiesFactory:
public class PropertiesFactory {
private static final String FILE = "springApplicationContext.properties";
private static PropertiesFactory propertiesFactory = new PropertiesFactory();
private InputStream in;
private Properties p;
public static PropertiesFactory getInstance() {
return propertiesFactory;
}
/**
* PropertiesFactory.
*
*/
private PropertiesFactory() {
super();
}
public void init() {
in = this.getClass().getClassLoader().getResourceAsStream(FILE);
}
public Object getPropertyValueOrProperties(final String property) {
try {
init();
p = new Properties();
p.load(in);
if (StringUtils.isNotEmpty(property))
return p.getProperty(property);
else
return p;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
springApplicationContext.properties:
url:구동 주소
driverClassName:드라이버 이름
username: 데이터베이스 이름
password: 데이터베이스 암호
packages:spring 스캔할 패키지
# JDBC Config
url=jdbc:mysql://localhost:3306/testUser
driverClassName=com.mysql.jdbc.Driver
username=root
password=root
#Hibernate Config
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true
hibernate.show_sql=true
#Spring Config
packages=org.lxh.bean