博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring中 JavaConfig和常见注解
阅读量:2079 次
发布时间:2019-04-29

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

跟杨春娟学SpringBoot笔记:Spring中 JavaConfig和常见注解

完成:第一遍

1.创建一个SpringFramework项目框架项目准备工作有哪些?

步骤一:File——》maven project

步骤二:勾选Create a simple project

步骤三:Group Id:com.adbycool

Artifact Id:JavaConfigProject
Name:JavaConfigProject
点finish

步骤四:点击pom.xml,直接add Spring-webmvc的jar包依赖

2.常见Java Config注解有哪些?

@Configuration注解:把当前文件配置成配置文件

@ComponentScan注解:把当前包和子包进行扫描
@PropertySource JDK版本高于等于JDK8的用法:把需要的properties文件引进
@PropertySources({@PropertySource(“classpath:file1.properties”)JDK版本低于JDK8的用法
@Import(ScanConfig.class):把相关文件配置到一起
@ImportResource(“classpath:applicationContext.xml”)

pom.xml

4.0.0
com.java.config.demo
JavaConfigProject
0.0.1-SNAPSHOT
Java Config Project
org.springframework
spring-webmvc
5.2.4.RELEASE

HelloWorld

package com.java.config.demo;public class HelloWorld {
public void sayHello() {
System.out.println("-----say hello"); }}

src/main/resources下的applicationContext.xml

APP

package com.java.config.demo;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.java.config.demo.scan.ScanHelloWorld;public class APP {
public static void main(String[] args) {
// TODO Auto-generated method stub /*ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld helloWorld = context.getBean("hello", HelloWorld.class); helloWorld.sayHello();*/ //@Configuration AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(HelloConfig.class); context.refresh(); HelloWorld helloWorld = context.getBean(HelloWorld.class); helloWorld.sayHello(); ScanHelloWorld scan = context.getBean(ScanHelloWorld.class); scan.sayHello(); //component-scan for xml file /*ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); ScanHelloWorld scan = context.getBean("scanHelloWorld", ScanHelloWorld.class); scan.sayHello();*/ /*AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(ScanConfig.class); context.refresh(); ScanHelloWorld scan = context.getBean(ScanHelloWorld.class); scan.sayHello();*/ }}

HelloConfig类来演示@Configuration注解

package com.java.config.demo;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;import org.springframework.context.annotation.ImportResource;@Configuration@Import(ScanConfig.class)@ImportResource("classpath:applicationContext.xml")public class HelloConfig {
@Bean public HelloWorld helloWorld() {
return new HelloWorld(); }}

ScanConfig类 来演示@ComponentScan注解

package com.java.config.demo;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.PropertySource;import org.springframework.context.annotation.PropertySources;@ComponentScan@PropertySource("classpath:file1.properties")@PropertySource("classpath:file2.properties")//jdk版本低于jdk8//@PropertySources({@PropertySource("classpath:file1.properties"), @PropertySource("classpath:file2.properties")})public class ScanConfig {
}

ScanHelloWorld

package com.java.config.demo.scan;import org.springframework.stereotype.Component;@Component("scanHelloWorld")public class ScanHelloWorld {
public void sayHello() {
System.out.println("--------scan"); }}

file1.properties

file2.properties
如果有多个配置文件.properties可以用
@PropertySource(“classpath:file1.properties”)
@PropertySource(“classpath:file2.properties”)
来配置

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

你可能感兴趣的文章
【雅思】雅思需要购买和准备的学习资料
查看>>
【雅思】雅思写作作业(1)
查看>>
【雅思】【大作文】【审题作业】关于同不同意的审题作业(重点)
查看>>
【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
查看>>
【English】【托业】【四六级】写译高频词汇
查看>>
【托业】【新东方全真模拟】01~02-----P5~6
查看>>
【托业】【新东方全真模拟】03~04-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST05~06-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST09~10-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST07~08-----P5~6
查看>>
solver及其配置
查看>>
JAVA多线程之volatile 与 synchronized 的比较
查看>>
Java集合框架知识梳理
查看>>
笔试题(一)—— java基础
查看>>
Redis学习笔记(三)—— 使用redis客户端连接windows和linux下的redis并解决无法连接redis的问题
查看>>
Intellij IDEA使用(一)—— 安装Intellij IDEA(ideaIU-2017.2.3)并完成Intellij IDEA的简单配置
查看>>
Intellij IDEA使用(二)—— 在Intellij IDEA中配置JDK(SDK)
查看>>
Intellij IDEA使用(三)——在Intellij IDEA中配置Tomcat服务器
查看>>
Intellij IDEA使用(四)—— 使用Intellij IDEA创建静态的web(HTML)项目
查看>>
Intellij IDEA使用(五)—— Intellij IDEA在使用中的一些其他常用功能或常用配置收集
查看>>