javaConfig&springBoot入门
1. javaConfig基础
1.1 为什么要学习javaConfig
因为:Springboot原理基于它的!!!(为学习springBoot打下基础)
1.2 Java 的 bean 配置(JavaConfig)出现历史
spring1.x:xml配置
spring2.x:注解配置(打注解,扫描注解)
spring3.x-4.x javaconfig&springboot
Spring5.x
2. JavaConfig操作
2.1 spring测试方式
方式一:new ClassPathXmlApplicationContext
/**
* 方式一:直接new
* @throws Exception
*/
@Test
public void test1() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
String[] beanDefinitionNames = context.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
System.out.println(beanDefinitionName);
}
}
直接new
方式二:注入:Runwith ContextConfigration
/**
* 方式二:注解方式 springtest注入
* @author Lenovo
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-xml.xml")
public class SpringTestTest {
@Autowired
private ApplicationContext applicationContext; @Test
public void test2() throws Exception {
for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) {
System.out.println(beanDefinitionName);
}
}
}
注解方式
2.2 xml配置
<bean id=”myDate” class=”java.util.Date”>
2.3 注解配置
1. 打注解
2. 扫描包
<context:component-scan base-package=”包的全限定名”/>
2.4 javaconfig配置
基本:
配置类:@Configration 代替了xml配置文件
@Bean 代替了<bean>
/**
* @Configuration 加上此注解就相当于一个spring配置文件(applicationContext.xml)
* @author Lenovo
*/
@Configuration
public class IocConfig {
/**
* @Bean 就相当于创建了一个bean
* 等同于 <bean id="myBean" class="....MyBean"/>
* @return
*/
@Bean
public MyBean myBean() {
return new MyBean();
}
}
基本的配置类
扫描包:
bean扫描(@ComponentScan/ComponentScans)
/**
* 注解方式配置bean
* 1.打注解
* 2.扫描包
*
* @author Lenovo
*/
@Configuration
/**扫描父类包
@ComponentScan("cn.itsource._05componentScan")
*//**扫描多个包的方式
@ComponentScans(value = {
@ComponentScan("cn.itsource._05componentScan.controller"),
@ComponentScan("cn.itsource._05componentScan.service")
})*///排除指定注解或者包含指定注解的bean
@ComponentScans(value = {
/*@ComponentScan(value = "cn.itsource._05componentScan", excludeFilters = {
//排除指定的注解
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})
})*/
//包含指定的注解
@ComponentScan(value = "cn.itsource._05componentScan", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Service.class})
}, useDefaultFilters = false )//关闭默认全部扫描includeFilters才生效)
})
public class IocConfig {}
ComponentScan扫描包
Bean详情:
//给value给bean取名如果没有设置默认就是方法名
@Bean(value = "myBean")
//singleton:单例 prototype:多例
@Scope(value = "singleton")
//懒加载(只对单例模式有用) 你用的时候才给你加载
@Lazy
bean详情
@Condition按条件注入:
1.条件类创建(实现Condition接口)
/**
* windows操作系统才能获取bean
* @author Lenovo
*/
public class WindowsCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
//获取类加载器
ClassLoader classLoader = conditionContext.getClassLoader();
//获取spring容器
ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
//获取注册器
BeanDefinitionRegistry registry = conditionContext.getRegistry();
//获取本机环境
Environment environment = conditionContext.getEnvironment();
String osName = environment.getProperty("os.name");
System.out.println("Windows:" + osName);
return osName.contains("Windows");
}
}
2.@Condition添加到类或者方法上面
@Bean
@Conditional(value = WindowsCondition.class)
public MyBean myBeanWindows() {
return new MyBean();
}
@Import导入bean:
创建bean的方式
方式1:@ComponentScan+注解(@Controller+@Service+@Repository+@Compont)-自己创建的bean
方式2:@Bean 别人的bean
方式3:@Import(快速向容器中注册一个bean)
1)@Import(要导入的组件),名称就是累的全限定名
2)ImportSelector:导入选择器,返回需要导入组件类的全限定名数组-springboot底层用的多
/**
* 选择器,
* @author Lenovo
*/
public class MyImportSelector implements ImportSelector {
/**
* 你要注册类全限定名数组
* @param annotationMetadata
* @return
*/
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
//可以做逻辑判断
return new String[]{"cn.itsource._08import_.PurpleColor", "cn.itsource._08import_.GrayColor"};
}
}
importSelector选择器
3)ImportBeanDefinitionRegistrar:通过bean定义注册器手动项目spring中容器中注册
/**
* 通过bean定义注册器手动项目spring中容器中注册
* @author Lenovo
*/
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
BeanDefinitionRegistry beanDefinitionRegistry) {
beanDefinitionRegistry.registerBeanDefinition("redColor", new RootBeanDefinition(RedColor.class));
}
}
ImportBeanDefinitionRegistar注册bean
@Import(value = {GreenColor.class, YellowColor.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
方式4:FactoryBean的方式,返回的是getObject的类实例-和其他框架集成是用的多
public class PersonFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
return new Person();
} @Override
public Class<?> getObjectType() {
return Person.class;
} @Override
public boolean isSingleton() {
return true;
}
}
factoryBean创建bean
3. springBoot入门
3.1 步骤
一 创建项目
parent
dependency
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
二 创建springboot项目并且启动
1)任意类加上@SpringBootApplication
2)Main函数启动springboot的应用
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
三 写一个Contorller来测试
HelloConroller
@Controller
public class HelloController { @RequestMapping("/hello")
@ResponseBody
public String hello() {
return "hello springBoot!";
}
}
3.2 打包
1.导入插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>配置类全限定名</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.打包
3. 运行
窗口运行:java -jar xxx.jar
后台运行: nohup java -jar XXX.jar & 只linux