- Spring 无法替代,Hibernate,Struts 可以换成其他框架
- 纯 Spring 可以建 Java 项目,与 Struts 交互需要 Java Web 项目
jar 包 链接:http://pan.baidu.com/s/1o8JV4wy 密码:tlb9
IOC - 依赖注入(控制反转)
帮开发者创建对象,新建对象可以用 ac.getBean 方法
方式1:配置 bean 标签,类必须写 get set 方法
spring-service.xml 中写类的 bean 标签
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="tea" class="com.shuai.ioc.Teacher">
<property name="name" value="Tom"></property>
</bean>
<!--getBean方法获取类-->
<bean name="stu" class="com.shuai.ioc.Student">
<property name="name" value="Shuai"></property>
<property name="teacher" ref="tea"></property>
</bean>
</beans>
测试
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-service.xml");
Student stu = (Student) ac.getBean("stu");
System.out.println(stu);
}
方式2:类加注解,不用写 get set 方法
Teacher 类
package com.shuai.ioc;
import org.springframework.stereotype.Component;
/**
* Created by shuai
* on 2017/8/22.
*/
@Component // 看这里
public class Teacher {
private String name;
public Teacher() {
}
public Teacher(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
'}';
}
}
Student 类
package com.shuai.ioc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
/**
* Created by shuai
* on 2017/8/22.
*/
//@Component
//@Service
//@Controller
@Repository // 看这里,4种都可以
public class Student {
private String name;
@Autowired
private Teacher teacher;
public Student() {
}
public Student(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", teacher=" + teacher.toString() +
'}';
}
}
spring-service.xml 中 只写 context 标签
<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--<bean class="com.shuai.ioc.Teacher">-->
<!--<property name="name" value="Tom"></property>-->
<!--</bean>-->
<!--<!–getBean方法获取类–>-->
<!--<bean class="com.shuai.ioc.Student">-->
<!--<property name="name" value="Shuai"></property>-->
<!--<property name="teacher" ref="tea"></property>-->
<!--</bean>-->
<context:component-scan base-package="com.shuai.ioc" />
</beans>
测试
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-service.xml");
Student stu = (Student) ac.getBean("student");
System.out.println(stu);
System.out.println(stu.getTeacher());
}
AOP - 面向切面编程
- 切面
- 切入点
- 连接点
- 织入
- 目标对象
- 代理对象
代理:把一些操作交给别人来做
代理模式
- 代理模式是常用的 Java 设计模式,它的特征是代理类与委托类有同样的接口
- 代理类主要负责 为委托类预处理消息、过滤消息、把消息转发给委托类,以及事后处理消息等。
- 代理类与委托类之间通常会存在关联关系,一个代理类的对象与一个委托类的对象关联,代理类的对象本身并不真正实现服务,而是通过调用委托类的对象的相关方法,来提供特定的服务。
注意:
- 委托类对象就是我们后面说到的 目标对象(需要【被】代理的对象)
- 代理类对象就是我们后面说到的 代理对象(目标对象就是需要这个对象做为代理)
SSH Spring - Struts2 - Hibernate
ssh 主要是把 Spring - Struts2 - Hibernate 三大框架结合在了一起。
项目的核心文件是 spring-service.xml
- 包含了 hibernate.cfg.xml 配置数据库的部分
- 包含了 Struts2 配置过滤器的部分(web.xml)
- 包含了 Spring 依赖注入(IOC)和 代理模式(AOP)bean, proxy...
<?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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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:component-scan base-package="com.shuai.dao"/>
<context:component-scan base-package="com.shuai.service"/>
<context:component-scan base-package="com.shuai.web.action"/>
<!-- Spring Hibernate 结合 -->
<!-- 可以不用要Hibernate.cfg文件 -->
<!-- 配置数据库 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 这些property对象class所指类的属性名 -->
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.1.112:1521:XE</value>
</property>
<property name="username">
<value>briup</value>
</property>
<property name="password">
<value>briup</value>
</property>
</bean>
<!-- 事务 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.shuai.bean</value> <!--实体所在的包-->
</list>
</property>
</bean>
<!-- 事务管理 相当于一个切面 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="txPointcut" expression="execution(* com.shuai.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>