spring的两大核心概念:IoC(控制反转)和DI(依赖注入)
控制反转:
在实际的开发中,我们应该尽量避免和降低对象间的依赖关系,即降低耦合度。通常的业务对象之间都是互相依赖的,业务对象与业务对象,业务对象与持久层,业务对象与各种资源之间都存在这样那样的依赖关系!怎样降低他们之间的依赖关系?IoC!在传统的实现中,由程序内部来控制程序之间的依赖关系,我们通常用new关键字来实现两组件间关系的组合,这种关系造成组件之间的耦合,IOC实现了组件的关系从程序内部提到外部容器来管理。也就是说由容器运行期间将组件间的某种依赖关系动态的注入组件中,控制程序间关系的实现交给了容器来完成!
控制反转的本质就是控制权由应用代码转到了外部容器,控制权的转移既是所谓的控制反转!
IoC实现策略:依赖注入,把符合依赖关系的对象通过属性(set和get)传递给需要的对象!
举个例子:软件设计方法和设计模式的发展,共产生了3中类调用的方法:自己创建,工厂模式,外部注入!
//book接口
public Interface IBook{ pubic void learn(); } //bookA实现类
public class BookA implements IBook{ pubic void learn(){ System.out.println("学习BookA"); } } //BookB实现类
public class BookB{ public void learn(){ System.out.println("学习BookB"); } }
|
自己创建:
public class Student{ public void learnBookA(){ IBook book=new BookA(); book.learn(); } public void learnBookB(){ IBook book=new BookB(); book.learn(); } } //测试运行
public class Test{ public static void main(String args[]){ Student student=new Student(); student.learnBookA(); student.learnBookB(); } }
|
工厂创建:
public class BookFactory{ public static IBook getBookA(){ IBook book=new BookA(); return book; } public static IBook getBookB(){ IBook book=new BookB(); return book; } } public class Student{ public void learnBookA(){ IBook book=BookFactory.getBookA(); book.learn(); } public void learnBookB(){ IBook book=BookFactory.getBookB(); book.learn(); } }
public class Test{ public static void main(String args[]){ Student student=new Student(); student.learnBookA(); student.learnBookB(); } }
|
外部注入:
public class Student{ public void learnBook(IBook book){ book.learn(); } }
public class Test{ public static void main(String aargs[]){ IBook bookA=new BookA(); IBook bookB=new BookB(); Student student=new Student(); student.learnBook(bookA); studetn.learnBook(bookB); } }
|
依赖注入(DI)
1.接口注入 2.构造注入 3.设置注入(setter)
设置注入:
pubic class Student{ private BookA bookA; public BookA getBookA(){ return bookA; } public void setBookA(BookA bookA){ this.bookA=bookA; } pubic void learn(){ bookA.learn(); } } 然后在ApplicationContext中添加如下的配置: <bean id="bookA" class="BookA"/> <bean id="student1" class="Student"> <property name="book" ref="bookA"/> </bean>
ApplicationContext ctx=new FileSystemXmlApplicationContext("applicationContext.xml"); Student student=(Student)ctx.getBean("studetn1"); student.learn();
|
介绍property中可以包含集合元素:
<bean id="" class=""> <!--保存在java.util.properties类中--> <property name="adminEmails"> <props> <prop key="adminstrator">tian_bian_fei@163.com</prop> <prop key="support">tianhhhhh@126.com</prop> <prop key="development">fei@163.com</prop> </props> </property> <!--保存java.util.List类中--> <property name="someList"> <list> <value>a list element</value> <rer bean="myDataSource"> </list> </property> <!--保存在java.util.Map类中--> <property name="someMap"> <map> <entry> <key> <value>yup an entry</value> </key> <value>just some string</value> </entry> <entry> <key> <value>yup a ref</value> </key> <ref bean="myDataSource"/> </entry> </map> </property> <!---java.util.Set-> <property name="someSet"> <set> <value>just some string</value> <ref bean="myDataSource/"> </set> </property> </bean>
|
ApplicationContext可以在应用程序中创建,也可以在web.xml中声明,不过与struts结合使用的时候,在struts-config.xml中声明,就省去的在web.xml中声明!
spring还有MVC功能,省略......
使用JdbcTemplate操作数据库
1.配置数据源和事务
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost/BBS?useUnicode=true&characterEncoding=UTF-8"></property> <property name="username" value="root"></property> <property name="password" value="ranran"></property> </bean>
|
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean>
|
2.创建用户表的POJO User.java
3.创建数据库操作类:UserDAO.java
4.配置UserDAO及其事务代理
使用HibernateTemplate操作数据库
1.配置数据源与上面一样
2.配置SessionFactory数据源
<bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>vo/Board.hbm.xml</value> <value>vo/Type.hbm.xml</value> <value>vo/Bbs.hbm.xml</value> <value>vo/User.hbm.xml</value> <value>vo/Answer.hbm.xml</value></list> </property></bean>
|
3.配置事务
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource"/> </property> </bean>
|
4.配置DAO组件:
<bean id="UserDAO" class="dao.UserDAO"> <property name="sessionFactory"> <ref bean="SessionFactory" /> </property> </bean>
|
5.配置DAO事务:
<bean id="UserDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="transactionManager"/> </property> <property name="target"> <ref local="UserDAO"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*"> PROPAGATION_REQUIRED </prop> <prop key="*"> PROPAGATION_REQUIRED,readOnly </prop> <prop key="save"> PROPAGATION_REQUIRED,readOnly </prop> </props> </property> </bean>
|
6.创建DAO类
public interface IUserDAO{ public void insertUser(User user); public User getUser(String userid); public List getUsers(); public void deleteUser(User user); }
public class UserDAO extends HibernateDaoSupport implements IUserDAO{ public void insertUser(User user){ getHibernateTemplate().saveOrUpdate(user); } public User getUser(String userid){ return (User)getHibernateTemplate().get(User.class,new Integer(userid)); } public List getUsers(){ return getHibernateTemplate().find("from User"); } public void deleteUser(User user){ getHibernateTemplate.delete(user); } }
|
阅读(1093) | 评论(0) | 转发(0) |