Spring-ioc(控制反转)
添加Spring依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.4</version>
</dependency>
创建Java bean
public class Hello {
private String str;
public Hello() {
System.out.println("=========init===========");
}
public void setStr(String str) {
System.out.println("set方法:"+str);
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
配置Spring
创建applicationContext.xml配置文件,并注册bean
<bean class="com.imars.pojo.Hello" id="hello">
这样就完成了对象放入了spring容器,对象的存取都由spring ioc容器接管!
测试:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Object hello = context.getBean("hello");
System.out.println(hello.toString());
属性注入
普通属性注入
方法一:构造方面注入
方法二:set方法注入
方法三:p名称空间注入
方法四:工厂模式注入和实例工厂模式
例如有些类的构造方法是私有的,只能通过工厂模式来创建对象。这时如果需要使用spring的容器管理这类bean,则需要使用工厂模式或实例工厂模式注入
复杂属性注入
1.对象注入<property name="address" ref="address" />
2.数组注入
<property name="books">
<array>
<value>三国</value>
<value>水浒</value>
<value>红楼梦</value>
<value>西游记</value>
</array>
</property>
<property name="card">
<map>
<entry key="id" value="123456" />
<entry key="bank" value="654321" />
</map>
</property>
<property name="info">
<props>
<prop key="学号">20210301</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
使用Java配置
总体来说,在Spring中。想要将一个Bean注入到Spring容器中,有三种方式:
1.使用xml(前文介绍)2.使用Java配置(通过Java将Bean注册到Spring容器中)3.自动化扫描(后文介绍)
新建一个名为SayHello的Bean:
public class SayHello {
public String sayHello(int age) {
return "hello " + age;
}
}
再新建一个java类名字任意,并在类上加@Configuration
注解
@Configuration
public class JavaConfig {
@Bean
SayHello sayHello(){
return new SayHello();
}
}
@Configuration
这个注解的意思表示这个类是一个配置类,类似于ApplicationContext.xml
@bean
注解表示这个方法的返回值注册到容器中,类似配置文件的
测试
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(JavaConfig.class);
SayHello sayHello = ctx.getBean("sayHello", SayHello.class);
System.out.println(sayHello.sayHello("Tom"));
自动化配置
自动化配置是在实际开发中大量使用的
自动化配置可以使用Java配置,也可以使用xml配置
Java配置
新建Java Bean
@Service
public class UserService {
public List getAllList(){
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
list.add("list"+i);
}
return list;
}
}
@service
表示这是一个@component
,意思是这个类可以在被扫描的时候注册到Spring容器中去。@ComponentScan("com.imars.pojo")
这个注解写在JavaConfig配置类上,表示要扫描的范围,默认是该类所在的包及子包。
测试:
public void t3(){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(JavaConfig.class);
UserService bean = ctx.getBean("userService", UserService.class);
System.out.println(bean.getAllList());
}
这里需要注意bean的名字,默认是Bean的首字母小写,例如UserService的名字的userService,要想指定的话,可以在@service
后写上名字(user
)
xml配置
<context:component-scan base-package="com.imars.pojo" />
条件注解与多环境切换
@Conditional
注解可以使满足某一条件的bean生效
@Profile
注解是对@Conditional
的一个封装,专用于程序切换运行环境时参数的变化
例:
新建一个Java bean(偷懒使用了lombok)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DataSource {
private String url;
}
在JavaConfig中,写下两个bean且名称一样,如果单独这样写,程序会报错,但使用了@Profile
注解指定开发环境后,程序只会运行其中一个bean
@Bean("data")
@Profile("dev")
DataSource decData(){
DataSource source = new DataSource();
source.setUrl(11111);
return source;
}
@Bean("data")
@Profile("prod")
DataSource prodData(){
DataSource source = new DataSource();
source.setUrl(22222);
return source;
}
测试:
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("prod");
ctx.register(JavaConfig.class);
ctx.refresh();
DataSource data = ctx.getBean("data", DataSource.class);
System.out.println(data);
ctx.getEnvironment().setActiveProfiles("prod");
这行代码可以指定运行环境,切换程序使用不同的bean
Spring-Aop(面向切面编程)
Aop是一种思想,是对面向对象编程的一种补充。
Aop就是在程序运行时,在不改变源码的情况下,动态的增强方法是功能,例如给程序增加日志、事务、数据库操作等。
Aop中常见的概念
| 概念 | 说明 |
| 切点(PointCut) | 添加代码的地方,位置 |
| 通知(Advice) | 在切点添加的代码(类中的一个方法) |
| 切面(Aspect) | 切点+通知(它是一个类) |
| 连接点(JointPoint) | 切点的定义(方法位置的描述) |
切面Aspect:
@Component
//表示是一个切面
@Aspect
public class LogAspect {
@Before("pointCut()")
public void before(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();
System.out.println(name + "方法开始执行了。。。");
}
@Pointcut("execution(* com.imars.aop.*.*(..))")
public void pointCut(){
}
}
@Pointcut("execution(* com.imars.aop.*.*(..))")
-----定义切点位置
通知Advice就是方法,具体要增加的代码,在方法上标记增加的方式(前置增强,后置增强。。。)




