1.使用类构造器
2.使用静态类工厂
3.使用实例类工厂
控制反转的实现-依赖注入
xml注入
注入其他bean
外部bean
内部bean (最好不要被其他bean使用)
注入属性
构造器注入
注解注入
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 在要注入的属性中使用@Resource 或set方法上(是javaee的注解方式 推荐使用) @Autowire @Qulifier(“person”)(Spring 的注解方式) 自动扫描装配 在相应的类上加上注解 @Service 服务类 @Repository dao类 @Controller 控制类 @Component 分类不清的类 @Scope(“prototype”) 随时创建 作用范围(单例/随时创建) @PostConstruct 初始化方法 AOP 切面 切入点 连接点 通知 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 基于XML开发 准备普通切面类 public class XMLPersonServiceAOP { //前置通知: public void doSomethingBefore(String name) { System.out.println("前置通知:"+name); } //后置通知 public void doSomethingAfter(String personName) { System.out.println("后置通知:"+personName); } //最终通知 public void doSomethingEnd() { System.out.println("最终通知:"); } //例外通知 public void doThrow(Exception e) { System.out.println("例外通知:"); } //环绕通知 public Object doAround(ProceedingJoinPoint pjp) throws Throwable{ //if(){//判断用户是否有权限 Object result=pjp.proceed(); //} System.out.println("环绕通知:"); return result; } } Xml配置 基于注解的方式开发 在xml中配置 @Aspect @Component public class PersonServiceAOP { @Pointcut("execution(* com.oseica.service.PersonService.*(..))") private void anyMehtod() {}// 声明一个切入点 @Before("anyMehtod() && args(name)")//前置通知: public void doSomethingBefore(String name) { System.out.println("前置通知:"+name); } @AfterReturning(pointcut="anyMehtod()",returning="personName")//后置通知 public void doSomethingAfter(String personName) { System.out.println("后置通知:"+personName); } @After("anyMehtod()")//最终通知 public void doSomethingEnd() { System.out.println("最终通知:"); } @AfterThrowing(pointcut="anyMehtod()",throwing="e") //例外通知 public void doThrow(Exception e) { System.out.println("例外通知:"); } @Around("anyMehtod()") public Object doAround(ProceedingJoinPoint pjp) throws Throwable{ //if(){//判断用户是否有权限 Object result=pjp.proceed(); //} System.out.println("环绕通知:"); return result; } } Spring与 JDBC 事务 Annotation的方式(注解) 文件Jdbc.properties 如下: driverClassName=org.gjt.mm.mysql.Driver url=jdbc\\:mysql\\://localhost\\:3306/itcast?useUnicode\\=true&characterEncoding\\=UTF-8 username=root password=123456 initialSize=1 maxActive=500 maxIdle=2 minIdle=1 Spring xml文件如下 xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 使用: 在类前加 @Transactional 对某方法的check例外进行回滚 @Transactional(runbackFor=Exception.Class) 对某方法不执行事务 @Transactional(propagation=Propagation.NOT_SUPPORTED) XML的事务配置 xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 集成 xml方式 Spring JAR 包 aspectjrt.jar aspectjweaver.jar cglib-nodep-2.1_3.jar common-annotations.jar commons-dbcp.jar commons-logging.jar commons-pool.jar log4j-1.2.15.jar spring.jar spring-webmvc-struts.jar bean.xml xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=false hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider 启动配置 Web.xml中 或在struts配置文件中 hibernate JAR包 antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar ehcache-1.2.3.jar ejb3-persistence.jar hibernate3.jar hibernate-annotations.jar hibernate-cglib-repack-2.1_3.jar hibernate-commons-annotations.jar hibernate-entitymanager.jar Struts1.2 Struts2 Annotation方式 xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=false hibernate.format_sql=false hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=false hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider 在类中使用注解 如上各知识点 spring提供的乱码解决方案 在web.xml中配置 open session in view