1.spring核心
Spring两大核心:基于工厂模式IOC(DI)和基于动态代理AOP。
IOC(DI):指控制器反转(依赖注入),原来要使用某个类对象实例是必须自己创建,使用spring后就不需要自己创建,由spring创建,需要时直接从spring中获取并且有依赖关系是会spring会通过反射自动注入。
AOP:就是不影响正常执行过程的前后加入额外的逻辑。比如权限,日志等,该执行的业务逻辑正常执行知识可以进行权限的判断核日志记录。
2.注解式编程
2.1. 创建项目并且导入pom
创建maven并导入一下内容:
org.springframework
spring-context
4.3.12.RELEASE
3.2.2. 配置类&Bean注解
@Configuration:加了这个注解的类就相当于传统的一个applicationContext-xxx.xml
@Bean:在标注了@Configuration的类里面的方式上面打上@bean就相当于在applicationContext-xxx.xml配置的一个
Dao的名字默认就是方法名,如果想改方法名使用@Bean(“beanName”)
测试类
public class JavaConfigTest {
@Test
public void test() throws Exception {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfig.class);
MyBean bean = applicationContext.getBean(MyBean.class);
System.out.println(bean);
}
}
配置类
@Configuration
public class ApplicationConfig {
@Bean
public MyBean myBean(){
return new MyBean();
}
}
2.3. @ComponentScan扫描bean
我们原来使用spring的使用不会在xml中一个一个配置bean,我们在再类上加上@Repository,@Service,@Controller @Component,并且注入时可以使用@AutoWired的注解注入。 这一切的功能都需要我们配置包扫描<context:component-scan base-package=“cn.itsource”/>.
然而现在注解驱动开发已经没有了配置文件,不能配置。但是提供了
@ComponentScan,我们可以在配置类上面加上这个注解也是一样,并且也能扫描配置包项目的相关注解,也能完成自动注入。
配置类
@Configuration
@ComponentScan
public class ApplicationConfig {
}
MybBean
@Component
public class MyBean {
}
2.4组件信息
1)@Scope单例测试
2)@Lazy懒加载
3)initMethod:初始化方法
4)destroyMethod:销毁方法
2.5. @Conditional-按照条件注册
1)放到类上面下面所有方法都生效,但是如果方法上加了优先级更高
2)可以加到方法上面,也可以加到类上面. 根据当前os.name的环境来判断
2.6. @Import
@Import()参数可以传入以下参数
- {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
2.7FactoryBean注册组件
public class MyfactoryBean implements FactoryBean<MyBean> {
public MyBean getObject() throws Exception {
return new MyBean();
}
public Class<?> getObjectType() {
return MyBean.class;
}
public boolean isSingleton() {
return true;
}
}
写一个实现类实现FactoryBean,然后通过MyfactoryBean 对象拿到mybean对象
3.组件生命周期
3.1 接口InitializingBean和DisposableBean
InitializingBean:afterPropertiesSet,设置完属性后调用
DisposableBean:容器关闭时调用
3.2 注解@PostConstruct @PreDestroy
放在方法上,@PostConstruct 在构造器执行之后执行,
@PreDestroy 在对象销毁之前执行
3.3 BeanPostProcessor(接口),bean后置处理器
Object postProcessBeforeInitialization:初始化之前调用
Object postProcessAfterInitialization:初始化之后调用
4.springboot
5.2.1. 搭建项目
1)创建springboot-parent(pom)
<dependencyManagement>
<dependencies>
<!--springboot版本管理,springboot相关模块引入是就不需要制定版本了-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2)创建springboot_01_helllo maven模块
<parent>
<artifactId>springboot_parent</artifactId>
<groupId>cn.itsource.springboot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
5.2.2. 编码测试
1)新建启动类(App – Main方法)
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
2)新建一个Controller类
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}
}
3)测试代码
运行:App
浏览器:http://localhost:8080/hello