标签:oracle数据库 package htm mon ack 简化 acl strong aop
上次说完ssh框架的搭建,这个框架本身没什么问题,但可以通过使用spring注解来使代码更加简洁、提高阅读性。
一、在原来的框架上简化了以下几点:
1. 原来的属性set注入改为注解
2. 原来的Connection(连接数据库)包改为jdbc.properties文件配置
3.由于加入hibernate之后我们不需要创建数据库,但是向数据库里插入数据还是得自己写,以下就是插入语句:
insert into news(title,content,begintime,username) values(‘XX‘,‘XX...‘,‘2012-03-01‘,‘xiaowen‘); insert into news(title,content,begintime,username) values(‘XX‘,‘XX...‘,‘2012-03-02‘,‘xiaohong‘); insert into news(title,content,begintime,username) values(‘XX‘,‘XX...‘,‘2012-03-03‘,‘xiaochen‘);
二、首先来看看原来的applicationContext.xml文件中的部分配置。
<bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype">
<property name="is" ref="myIndexService"></property>
</bean>
<bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
<property name="id" ref="myIndexDao"></property>
</bean>
<bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype">
<property name="sf" ref="mySessionFactory"></property>
</bean>
从以上代码可以看出来,action控制层访问的是service业务层,而service业务层则是访问dao数据层。
每个类有什么属性要注入都显得非常明显,所以现在要做的就是把applicationContext.xml配置文件进行简化。
接着我们把applicationContext.xml文件中的代码简化成如下代码
<bean id="myIndexAction" class="ssh.action.IndexAction" scope="prototype"></bean> <bean id="myIndexService"class="ssh.service.IndexServiceImpl" scope="prototype"></bean> <bean id="myIndexDao" class="ssh.dao.IndexDaoImpl" scope="prototype"></bean>
在这里只是绑定了每个bean,并没有为其注入属性。其实是用到了Spring的@Autowired和@Qualifier这两个注解。
//属性的注解
@Autowired
@Qualifier("mySessionFactory")
private SessionFactory sf;
在Qualifier注解中我们申明其引用的是哪一个bean,Spring 便会自动为其注入这个实例,并且可以省略属性的set方法。
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 基于ssh这个包自动扫描其中的类 ,也会自动注入解析器-->
<context:component-scan base-package="ssh"></context:component-scan>
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
</bean>
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
</bean>
</beans>
从上面的applicationContext.xml文件中的代码我们可以看到配置文件没有对Java bean 进行相关配置, 但多了一行代码:
<context:component-scan base—package="ssh"></context:component-scan>,通过这个节点的base-package属性可以配置Spring需要自动注入的哪个基包。
接下来我们使用spring注解来对类进行注解: @Controller action层 、 @Service service层 、 @Repository dao层;
//action类的类注解
@Controller("myIndexAction")
@Scope("prototype")
public class IndexAction extends ActionSupport {
//使用spring内置注解@Autowired自动注入实例
@Autowired
//@Qualifier("myIndexService")
private IndexService is;
//Service类的类注解
@Service("myIndexService")
@Scope("prototype")
public class IndexServiceImpl implements IndexService {
//使用spring内置注解@Autowired自动注入实例
@Autowired
//@Qualifier("myIndexDao")
private IndexDao id;
//dao类的类注解
@Repository("myIndexDao")
@Scope("prototype")
public class IndexDaoImpl implements IndexDao {
//使用spring内置注解@Autowired自动注入实例
@Autowired
//@Qualifier("mySessionFactory")
private SessionFactory sf;
@Controller("myIndexAction")相当于applicationContext.xml配置文件中的bean节点 ,而括号中的值相当于bean节点中的id属性值。
则注解 @Scope("prototype")相当于applicationContext.xml文件中bean节点中scope属性,这个非单例 模式注解十分重要,主要起到线程安全,防止并发操作时出现异常的作用。
三、src目录下配置jdbc.properties(外部属性文件)
<!-- mysql数据库 -->jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/news jdbc.user=root jdbc.password=123456 <-- oracle数据库 --> #oracle jdbc_oracle.driver=oracle.jdbc.driver.OracleDriver jdbc_oracle.url=jdbc:oracle:thin@127.0.0.1:1521:orcl jdbc_oracle.user=news jdbc_oracle.password=123456
这样整个简化就完成了,如果有什么地方遗漏了欢迎大家补充
标签:oracle数据库 package htm mon ack 简化 acl strong aop
原文地址:http://www.cnblogs.com/pengziqiang/p/5994209.html