ssh整合之annotion

        前面是配置文件版的ssh整合,如今來講註解版的,註解版的會更加方便,不用來回切換到配置文件中,在代碼中就能夠實現配置。java

        使用註解只需在applicationContext.xml配置文件中,spring

        1.添加bean的自動掃描與裝配sql

<context:component-scan base-package="com.bjsxt"></context:component-scan>session

便會自動去掃描com.bjsxt下面或子包中的java文件,若是掃描到例如:app

            dao-@Repository("userDao")
            service-@Service("userService")
            action-@Controller("userAction") @Scope("prototype")
            屬性的注入:@Autowired @Qualifier("userService")
                    @Resourcessh

註解就會自動幫你配置bean以及相應屬性了。ide

        2.配置aop註解ui

            <tx:annotation-driven transaction-manager="txManager"/>
            <aop:aspectj-autoproxy></aop:aspectj-autoproxy>this

        3.配置hibernate註解
            <property name="packagesToScan">
                <list>
                    <value>com.bjsxt.vo</value>
                </list>
            </property>url

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:aop="http://www.springframework.org/schema/aop"
    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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 導入db.properties配置文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- 
		自動掃描和裝配
			dao-@Repository("userDao")
			service-@Service("userService")
			action-@Controller("userAction") @Scope("prototype")
			屬性的注入:@Autowired @Qualifier("userService")
					@Resource
	 -->
	<context:component-scan base-package="com.bjsxt"></context:component-scan>
	<!-- 配置數據源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc_driverClassName}"></property>
		<property name="url" value="${jdbc_url}"></property>
		<property name="username" value="${jdbc_username}"></property>
		<property name="password" value="${jdbc_password}"></property>
	</bean>
	<!-- 聲明式事務管理 -->
	<!-- 配置事務管理器 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 配置事務通知 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="save" propagation="SUPPORTS"/>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="update" propagation="REQUIRED"/>
			<tx:method name="delete" propagation="REQUIRED"/>
			<tx:method name="select" propagation="REQUIRED"/>
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	<!-- 配置aop註解方式 -->
	<tx:annotation-driven transaction-manager="txManager"/>
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<!-- 定義hibernate的SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 依賴注入數據源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 自動掃描註解方式配置hibernate類文件 -->
		<property name="packagesToScan">
			<list>
				<value>com.bjsxt.vo</value>
			</list>
		</property>
		<!-- 定義hibernate的SessionFactory的屬性 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
			</props>
		</property>
	</bean>
	<!-- 定義HibernateDaoSupport -->
	<bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate4.support.HibernateDaoSupport"
		abstract="true">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
</beans>

User.java

@Entity
@Table
public class User {
	private int id;
	private String name;
	private String pwd;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
}

UserServiceImpl.java

@Service("userService")
@Transactional
public class UserServiceImpl implements UserServiceI {
	private UserDaoI userDao;
	
	@Autowired
	public void setUserDao(UserDaoI userDao) {
		this.userDao = userDao;
	}

	@Override
	public List<User> getAll() {
		return userDao.getAll();
	}

}

UserDaoImpl.java

@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDaoI {

	@Override
	public List<User> getAll() {
		return (List<User>) this.getHibernateTemplate().find("from User");
	}
	
	@Autowired
	private void setSession(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

}

UserAction.java

@Controller("userAction")
@Scope("prototype")
public class UserAction {
	private UserServiceI userService;
	private List<User> list;
	
	public String list() {
		list = userService.getAll();
		return "success";
	}
	
	//required=true 不容許注入對象爲null
	//required=false 容許注入對象爲null
	@Autowired @Qualifier("userService")
	public void setUserService(UserServiceI userService) {
		this.userService = userService;
	}

	public List<User> getList() {
		return list;
	}

	public void setList(List<User> list) {
		this.list = list;
	}
}
相關文章
相關標籤/搜索