Spring初学者总结:搭建spring环境

本文详细介绍了Spring框架的基础概念、配置方式及面向切面编程的应用。包括使用配置文件和注解方式实现依赖注入,以及如何利用AOP进行代码增强。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring轻量级框架, Java EE的春天,当前主流框架,目标使现有技术更加易用,推进编码最佳实践。Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。

一.采用配置文件实现:

1. 导包:

 

包的解释:

 

2. 创建dao->>定义dao中要实现的方法->>创建dao.impl->>实现接口方法:

public class UserInfoDaoImpl implements UserInfoDao {

@Override

public void save() {

System.out.println("Spring保存成功!");

}

 

}

 

 

3. 创建service->>定义service中要实现的方法->>创建service.impl->>实现接口中的方法:

public class ServiceImpl implements UserInfoService {

private UserInfoDao userInfoDao;

       

public UserInfoDao getUserInfoDao() {

return userInfoDao;

}

 

public void setUserInfoDao(UserInfoDao userInfoDao) {

this.userInfoDao = userInfoDao;

}

 

@Override

public void save() {

userInfoDao.save();

}

 

}

 

 

4. 配置application.xml文件:

<?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.xsd">

<!-- 使用类构造器实例化(默认无参数)采用setter方法注入bean -->

<bean id="dao" class="dao.impl.UserInfoDaoImpl"></bean>

<bean id="service" class="service.impl.ServiceImpl">

<property name="userInfoDao" ref="dao"></property>

</bean>

</beans>

扩展:

 

5. 创建测试类:

public class TestSpring {

@Test

public void testSpring() {

//实例化Spring容器,ApplicationContext 应用上下文,加载Spring 框架配置文件

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

//通过 getBean方法获得Spring容器管理Bean对象

UserInfoService service=(UserInfoService) context.getBean("service");

service.save();

}

}

 

关系总结:

 

二.采用注解方式实现:

注解方式将Bean的定义信息和Bean实现类结合在一起,Spring提供的注解有

@Component

@Repository :用于标注DAO

@Service :用于标注业务类

@Controller :用于标注控制器类

 

1. 导包:

 

2. 创建dao->>定义dao中要实现的方法->>创建dao.impl->>实现接口方法:

package dao.impl;

 

import org.springframework.stereotype.Repository;

 

import dao.UserInfoDaoAnn;

@Repository("userInfoDaoAnn")//定义别名,与接口名一致,将首字母改成小写

public class UserInfoDaoImplAnn implements UserInfoDaoAnn {

 

@Override

public void save() {

System.out.println("SpringAnn保存成功!");

 

}

 

}

 

 

3. 创建service->>定义service中要实现的方法->>创建service.impl->>实现接口中的方法:

package service.impl;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Service;

 

import dao.UserInfoDaoAnn;

import service.UserInfoServiceAnn;

@Service("userInfoServiceImplAnn")

public class UserInfoServiceImplAnn implements UserInfoServiceAnn {

@Autowired   //自动注解方式,默认按照类型进行注入

@Qualifier("userInfoDaoAnn")//dao.impl中定义的名字

private UserInfoDaoAnn userInfoDaoAnn;

public UserInfoDaoAnn getUserInfoDaoAnn() {

return userInfoDaoAnn;

}

 

public void setUserInfoDaoAnn(UserInfoDaoAnn userInfoDaoAnn) {

this.userInfoDaoAnn = userInfoDaoAnn;

}

 

@Override

public void save() {

userInfoDaoAnn.save();

}

 

}

 

扩展:

 


 


4. 配置applicationAnn.xml文件:

<?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.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

">

<!-- 扫描包 -->

<context:component-scan base-package="dao,service" />

 

</beans>

 

5. 创建测试类:

@Test

public void testSpringAnn() {

//实例化Spring容器,ApplicationContext 应用上下文,加载Spring 框架配置文件

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextAnn.xml");

//通过 getBean方法获得Spring容器管理Bean对象

UserInfoServiceAnn service=(UserInfoServiceAnn) context.getBean("userInfoServiceImplAnn");

service.save();

}

关系总结:

 

三.Aop实现(前后置增强)

所谓面向切面编程,是一种通过预编译和运行期动态代理的方式实现在不修改源代码的情况下给程序动态添加功能的技术

1. 添加包

2. 创建要增强的功能(创建aop->>aop中创建LogAop.java->>写要添加的功能方法)

package aop;

 

public class LogAop {

public void before() {

System.out.println("前置增强");

}

public void after() {

System.out.println("后置增强");

}

public void around() {

System.out.println("环绕增强");

}

 

}

 

 

3. application.xml配置aop

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

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.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

">

<!-- 使用类构造器实例化(默认无参数) -->

<bean id="dao" class="dao.impl.UserInfoDaoImpl"></bean>

<bean id="service" class="service.impl.ServiceImpl">

<property name="userInfoDao" ref="dao"></property>

</bean>

<bean id="aop" class="aop.LogAop"></bean>

<aop:config>

<!-- 定义切入点 -->

<aop:pointcut id="pointcut" expression="execution(public void save(..))" />

<!-- 定义切面 -->

<aop:aspect ref="aop">

<aop:before method="before" pointcut-ref="pointcut"/><!-- 配置前置增强 -->

<aop:after method="after" pointcut-ref="pointcut"/><!-- 配置后置增强 -->

<!-- <aop:around method="around" pointcut-ref="pointcut"/> --><!-- 配置环绕增强 -->

</aop:aspect>

</aop:config>

<!-- 常用属性

pointcut:配置切入点表达式

pointcut-ref:配置切入点引用对象

method:配置切入点执行的通知方法

 -->

</beans>

解析:

1)切面(Aspect

切面由切点和增强(引介)组成,它既包括了横切逻辑的定义,也包括了连接点的定义,Spring AOP就是负责实施切面的框架,它将切面所定义的横切逻辑织入到切面所指定的连接点中。

2)切入点:简单的说,就是连接点的查询条

切入点的表达式匹配规则举例:

public * save(entity.User):“*”表示匹配所有类型的返回值。

public void *(entity.User):“*”表示匹配所有方法名。

public void save (..):“..”表示匹配所有参数个数和类型。

* service.*.*(..):匹配service 包下所有类的所有方法。

* service..*( ..):匹配service 包及子包下所有类的所有方法。

3)代理(Proxy

一个类被AOP织入增强后,就产出了一个结果类,它是融合了原类和增强逻辑的代理类。根据不同的代理方式,代理类既可能是和原类具有相同接口的类,也可能就是原类的子类,所以我们可以采用调用原类相同的方式调用代理类。

 

4. 测试类test.java

@Test

public void testSpringAOp() {

//实例化Spring容器,ApplicationContext 应用上下文,加载Spring 框架配置文件

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");

//通过 getBean方法获得Spring容器管理Bean对象

UserInfoService service=(UserInfoService) context.getBean("service");

service.save();

}

关系总结:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值