Spring入门总结

mac2025-06-25  7

一、Spring优势    1、方便解耦,方便开发    2、AOP编程的支持    3、声明式事务的支持    4、方便程序的测试    5、方便集成各种优秀框架    6、降低javaEE API的使用难度    7、Java源码是经典学习范例

二、Bean实例化三种方式    无参构造方法实例化  使用最多    工厂静态方法实例化 factory-method=''    工厂实例方法实例化  factory-bean='',factory-method=''

三、控制反转IOC (1)、导入坐标 <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-context</artifactId>             <version>5.0.5.RELEASE</version> </dependency>  <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-test</artifactId>             <version>5.0.5.RELEASE</version>  </dependency>(2)、注入方式 applicationContext.xml下配置 //id对象名称   class对象对应的相对路径 <bean id="userDao" class="dao.impl.userDaoImpl"></bean> 注入类型    a) set方法  <property name="XX" ref="XX">         name :  属性名称         value:   普通属性值         ref :       注入的对象引用值         <list>               <map>         <properties>          <constructor-arg> <property>    b) p名命空间注入 xmlns:p="http://www.springframework.org/schema/p" <bean id="X" class="X" p:userDao-ref="X" />    c) 构造方法   <constructor-arg name="X" ref="X"></constructor-arg>

(3)、注解方式 <!--开启组件扫描-->   <!--扫描包及其子包 context需要在schema中引入才能使用-->    <context:component-scan base-package="com.itheima" /> 旧注解 @Component    创建对象 @Controller @Service @Respository @Autiwired            根据字段类型依赖注入 @Qualifier              结合@Autowired一起使用,根据名称进行依赖注入 @Resource             相当于@Autowired +@Qualifier @Value                   注入普通属性 @Scope                  标注bean的作用范围 @PostConstruct      对象的初始方法注入 @PreDestroy           对象的销毁方法注入 新注解 @Configration 用于指定当前类是一个Spring配置类,当创建容器时会从该类上加注解 @ConponentScan 用于指定Spring在初始化容器时要扫描的包 @Bean 使用在方法上,标注将该方法的返回值存储到Spring容器中 @PropertySource      用于加载.properties文件中的配置 @import  用于导入其他配置文件

Spring集成Junit步骤 导入spring继承Junit的坐标 使用@Runwith注解替换原来的运行期 使用@ContextConfiguration指定配置文件或配置类 使用@Autowired注入需要测试的对象 创建测试方法进行测试

(4)、引入其他Spring配置文件 <import resource="xxx.xml" />

四、面向切面编程AOP  面向切面编程,是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

1、AOP的作用及其优势 作用:在程序运行期间,不修改源码的情况下对方法进行功能增强 优势:减少代码重复性,提高开发效率,并且便于维护 常用的动态代理技术     JDK代理:基于接口的动态代理技术     cglib代理:基于父类的动态代理技术

2、AOP的相关术语 Target(目标对象):代理的目标对象 Proxy(代理):一个类被AOP织入增强后,产生的代理类 Joinpoint(连接点):所谓连接点是指那些被拦截到的点,在Spring中,这些点指的是可以被增强的方法 Pointcut(切入点):所谓的切入点指的是我们要对哪些Jointpoint进行拦截的定义      公民都有机会成为人大代表,但是公民不是人大代表。人大代表就是切入点,公民是连接点 Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情 Aspect(切面):是切入点和通知(引介)的结合 Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入

3、AOP开发明确的事项    导入aop相关坐标 aspectjweaver    创建目标接口和目标类    创建切面类(内部有增强方法)    将目标类和切面类的对象创建权交给spring    在applicationContext.xml中配置织入关系    测试代码

4、具体内容 1、导入坐标 <dependency>             <groupId>org.aspectj</groupId>             <artifactId>aspectjweaver</artifactId>             <version>1.8.4</version>  </dependency> 2、编写业务(目标)类,和切面(增强)类 3、配置applicationContext.xml //增强(切面)对象 <bean id="myAspect" class="aop.MyAspect" /> //目标对象 <bean id="target" class="aop.targetImpl"/> //aop配置     <aop:config>         //配置切点   哪些需要被增强,可以有多个   切点表达式的写法execution(* aop.*.*(..))         <aop:pointcut id="point" expression="execution(* aop.*.*(..))"/>         //配置切面         <aop:aspect ref="myAspect">             //before为增强前   method="before"为切面中的方法             <aop:before method="before" pointcut-ref="point" />         </aop:aspect>     </aop:config>

补充:切面中的环绕增强 //需要传参 (ProceedingJoinPoint 对象 public Object around(ProceedingJoinPoint pjp) throws Throwable {         System.out.println("环绕前增强");         Object proceed = pjp.proceed();         System.out.println("环绕后增强");         return proceed;     }

4、注解配置 切点表达式抽取   1、在切面类中创建一个方法,加注解pointcut @Pointcut("excution(* XX.*.*(..))") public  void pointcut(){}   2、引用,2种方法       @Around("pointcut()")       @Around("类名.pointcut()") 注解开发步骤     使用@Aspect标注切面类     使用@通知注解标注通知方法     在配置文件中配置aop自动代理 <aop:aspectj-autoproxy />

五、事务tx JDBCTemplate开发步骤 1、导入spring-jdbc和spring-tx坐标 <dependency>             <groupId>c3p0</groupId>             <artifactId>c3p0</artifactId>             <version>0.9.1.2</version>         </dependency> <dependency>             <groupId>com.alibaba</groupId>             <artifactId>druid</artifactId>             <version>1.1.10</version>         </dependency> <dependency>             <groupId>mysql</groupId>             <artifactId>mysql-connector-java</artifactId>             <version>5.1.32</version>         </dependency> <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-jdbc</artifactId>             <version>5.0.5.RELEASE</version>         </dependency> <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-tx</artifactId>             <version>5.0.5.RELEASE</version>         </dependency> 2、创建数据库表和实体 3、创建JdbcTemplate对象 4、执行数据库操作 

声明式事务控制的配置要点 平台事务管理器配置 事务通知的配置 事务aop织入的配置

六、web 无需写new ClassPathXmlContext("XX.xml")代码,直接将对象放在servletContext域中,可以用@Control直接创建 1、导入坐标 <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-web</artifactId>             <version>5.0.5.RELEASE</version>         </dependency> 2、web.xml配置 <!--配置spring-->     <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>classpath:applicationContext.xml</param-value>     </context-param>     <listener>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     </listener>

最新回复(0)