博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring总结
阅读量:3941 次
发布时间:2019-05-24

本文共 4655 字,大约阅读时间需要 15 分钟。

Sring总结

配置注入

  1. 普通类型
  1. 集合数据类型(List)的注入
aaa
bbb
ccc
  1. 集合数据类型(List)的注入
  1. 集合数据类型( Map<String,User> )的注入
  1. 集合数据类型(Properties)的注入
aaa
bbb
ccc

注解使用

spring原始注解

注解 说明
@Component 使用在类上用于实例化Bean
@Controller 使用在web层类上用于实例化Bean
@Service 使用在service层类上用于实例化Bean
@Repository 使用在dao层类上用于实例化Bean
@Autowired 使用在字段上用于根据类型依赖注入
@Qualifier 结合@Autowired一起使用用于根据名称进行依赖注入
@Resource 相当于@Autowired+@Qualifier,按照名称进行注入
@Value 注入普通属性
@Scope 标注Bean的作用范围
@PostConstruct 使用在方法上标注该方法是Bean的初始化方法
@PreDestroy 使用在方法上标注该方法是Bean的销毁方法

spring新注解

注解 说明
@Configuration 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解
@ComponentScan 用于指定 Spring 在初始化容器时要扫描的包。 作用和在 Spring 的 xml 配置文件中的 <context:component-scan base-package=“com.itheima”/>一样
@Bean 使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@PropertySource 用于加载.properties 文件中的配置
@Import 用于导入其他配置类

Spring集成Junit步骤

①导入spring集成Junit的坐标

②使用@Runwith注解替换原来的运行期

③使用@ContextConfiguration指定配置文件或配置类

④使用@Autowired注入需要测试的对象

⑤创建测试方法进行测试

注解aop开发步骤

①创建目标接口和目标类(内部有切点)

public interface TargetInterface {
public void method();}public class Target implements TargetInterface {
@Override public void method() {
System.out.println("Target running...."); }}

②创建切面类(内部有增强方法)

public class MyAspect {
//前置增强方法 public void before(){
System.out.println("前置代码增强....."); }}

③将目标类和切面类的对象创建权交给 spring

@Component("target")public class Target implements TargetInterface {
@Override public void method() {
System.out.println("Target running...."); }}@Component("myAspect")public class MyAspect {
public void before(){
System.out.println("前置代码增强....."); }}

④在切面类中使用注解配置织入关系

@Component("myAspect")@Aspectpublic class MyAspect {
@Before("execution(* com.itheima.aop.*.*(..))") public void before(){
System.out.println("前置代码增强....."); }}

⑤在配置文件中开启组件扫描和 AOP 的自动代理

⑥测试代码

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class AopTest {
@Autowired private TargetInterface target; @Test public void test1(){
target.method(); }}

SpringMVC的数据响应-回写数据-返回对象或集合(应用)

通过SpringMVC帮助我们对对象或集合进行json字符串的转换并回写,为处理器适配器配置消息转换参数,指定使用jackson进行对象或集合的转换,因此需要在spring-mvc.xml中进行如下配置:

SpringMVC的数据响应-知识要点小结

1) 页面跳转

直接返回字符串

通过ModelAndView对象返回

2) 回写数据

直接返回字符串

HttpServletResponse 对象直接写回数据,HttpServletRequest对象带回数据,Model对象带回数据或者@ResponseBody将字符串数据写回

返回对象或集合

配置工程

01-环境搭建

①创建工程(Project&Module)

②导入静态页面

③导入需要坐标(pom.xml)

④创建包结构(controller、service、dao、domain、utils)

⑤导入数据库脚本(.sql)

⑥创建POJO类

创建配置文件(applicationContext.xml,spring-mvc.xml,jdbc.properties,log4j.properties)

配置文件

  1. 创建Spring的核心配置文件applicationContext.xml
  1. 创建SpringMVC核心配置文件spring-mvc.xml
  1. 创建数据库配置文件jdbc.properties

  2. 拷贝日志配置文件log4j.properties

  3. web.xml

CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
enoding
UTF-8
CharacterEncodingFilter
/*
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
DispatcherServlet
/

完成功能

①点击角色管理菜单发送请求到服务器端(修改角色管理菜单的url地址)

②创建Controller和list()方法

③创建Service和list()方法

④创建Dao和findAll()方法

⑤使用JdbcTemplate完成查询操作

⑥将查询数据存储到modelAndView中

⑦转发到.jsp页面进行展示

转载地址:http://ennwi.baihongyu.com/

你可能感兴趣的文章
Linux域名IP映射
查看>>
Java的反射机制
查看>>
SpringCloud微服务应用入门
查看>>
SpringCloud之session共享
查看>>
Springboot集成Shiro实现认证
查看>>
Spring、Spring MVC和MyBatis编程式集成示例
查看>>
在Springboot应用使用redis缓存
查看>>
Spring入门
查看>>
Idea提示键和热部署配置以及git使用
查看>>
Deepin+Vscode搭建vue.js项目及Git操作
查看>>
基于Spring Security前后端分离式项目解决方案
查看>>
Vue3.0+Vite2.0项目框架搭建(一)
查看>>
Vue3.0+Vite2.0项目框架搭建(二)- 引入axios
查看>>
Vue3.0+Vite2.0项目框架搭建(三)- 引入Element3
查看>>
使用Vue CLI v4.5(+)搭建Vue3.0项目框架搭建
查看>>
Java集合框架
查看>>
线程协作与生产者消费者问题
查看>>
Vue入门
查看>>
非starter方式实现springboot与shiro集成
查看>>
Starter方式实现Springboot与Shiro集成
查看>>