一、IOC与DI与AOP
1.什么是IOC?
Inversion of control 控制反转
比如以前创建一个对象,需要自己主动new 一个。通过IOC,对象的创建交由Spring框架创建,开发人员直接使用已经创建好的对象
( 简单说,就是交由Spring创建对象)
( 1 ) Person类
package com. hua. pojo;
public class Person {
private String name;
private int age;
}
( 2 ) applicationContext. xml配置
< beans
< bean name= "ps" class = "com.hua.pojo.Person" >
< property name= "name" value= "张三" / >
< / bean>
< / beans>
applicationContext. xml是核心配置文件,通过关键字p即可获取Person类对象,该对象
获取的时候,即被注入了字符串“张三”到name属性
( 3 ) 获取Person类对象及被注入的属性
ApplicationContext ctx= new ClassPathXmlApplicationContext ( new String [ ] { "applicationContext.xml" } ) ;
Person person= ( Person) ctx. getBean ( "ps" ) ;
String name= person. getName ( ) ;
2.什么是DI?
Dependency Injection 依赖注入
通过IOC创建对象的时候,可以注入字符串甚至其它对象,例如DAO就会注入Session Factory
( 简单说就是拿到对象的的属性,已经被注入好相关值,直接使用即可)
通常IOC和DI是紧密结合,一起使用的
3.什么是AOP
Aspect Oriented Programming 面向切面编程
把功能划分为核心业务功能和其它的周边辅助功能,比如日志、性能统计、事物等等。
其它周边辅助功能可以都看作切面功能。
核心功能和切面功能分别独立开发,通过面向切面编程,可以有机的把核心业务功能和切面功能根据需求结合在一起。
比如增加操作可以和事物切面结合在一起,查询操作可以和性能统计切面结合在一起
在配置方面,要配置切面,切点,并且通过aspect:config把切面和切点结合起来
二、注解
1.@Configuration
可以看作一个Spring的XML配置文件
本身已具有@Component 功能
2.@Bean
可以看作XML文件中的一个< Bean> 元素
示例:
public class Person {
}
@Configuration
public class MyConfig {
@Bean
public Person ps ( ) {
return new Person ( ) ;
}
@Bean ( "person" )
public Person ps2 ( ) {
return new Person ( ) ;
}
}
( 没传入bean名称则默认使用方法名作为名称)
@Controller
public class MyController {
@Autowired
ApplicationContext ctx;
@GetMapping ( "/test" )
public void test ( ) {
String[ ] arr= ctx. getBeanNamesForType ( Person. class ) ;
for ( String name: arr) {
System. out. println ( name) ;
}
}
}
结果:
ps
person
( Spring容器中有2 个Person类型的bean)
3.@Resource
根据名称进行注入
@Resource ( "MyPsService" )
PersonService service;
4.@Autowired
默认根据类型进行注入
@Autowired
PersonService service;
( 根据属性名"service" 来定位是哪一个PersonService)
5.@Qualifier
指定注入名称的bean
6.@Aspect
添加依赖:
< dependency>
< groupId> org. aspectj< / groupId>
< artifactId> aspectjweaver< / artifactId>
< / dependency>
package com. hua. aop;
@Compoent
public class SaleServiceImpl {
public void sell ( ) {
System. out. println ( "执行销售业务方法" ) ;
}
}
@Aspect
@Component
public class SaleAspect {
@Before ( "execution(* com.hua.aop.*ServiceImpl.*(..))" )
public void before ( ) {
System. out. println ( "业务方法执行前" ) ;
}
@After ( "execution(* com.hua.aop.*ServiceImpl.*(..))" )
public void after ( ) {
System. out. println ( "业务方法执行后" ) ;
}
}
public static void main ( String[ ] args) {
new SpringApplicationBuilder ( ProxyApp. class ) . properties ( "spring.aop.proxy-target-class=true" ) . run ( args) ;
}
@Controller
public class MyController {
@Autowired
SaleServiceImpl saleService;
@GetMapping ( "/test" )
public void test ( ) {
saleService. sell ( ) ;
}
}
请求http: / / localhost: 8080 / test,输出:
业务方法执行前
执行销售业务方法
业务方法执行后
7.@Controller
对于用@Controller 声明的对象,Spring会认为该对象是个Web Controller
若该对象的方法中有@RequestMapping 注解,则会将客户端发起的http请求转化为Java方法调用
例如,对于客户端发起的/ login. htm请求,会执行Controller中的login ( ) 方法,并自动将请求参数按照login ( ) 方法声明的名词一一对应上
8.RequestMapping
注解类或方法,用于映射请求与处理方法
9.PathVariable
修饰方法的参数,将请求URI地址中的变量转化为方法的参数
@RequestMapping ( value= "/test/{myName}" )
public void test ( @PathVariable ( "myName" ) String name) {
}
10.RequestParam
使用@RequestParam 来获取请求中的参数,常见于表单提交
@RequestMapping ( value= "/login" )
public void login ( @RequestParam String userName) {
}
< form method= "post" action= "http://localhost:8080/login" >
< input type= "text" name= "userName" / >
< input type= "submit" value= "提交" / >
< / form>
11.RestController
相当于@ResponseBody 和@Controller 的结合
方法返回值将会被写到http的响应体中。
所返回的内容既不放到模型中,也不会被解释为视图名称