4.以注解方式开发bean
2019-03-01 本文已影响0人
youi_e050
1、HelloWorld的例子改成用注解的方法实现
- Student类采用@Component注解
package com.spring.annotation;
import org.springframework.stereotype.Component;
/**
* 采用注解开发的bean
* @Component用于类级别注解,标注本类为一个可被Spring容器托管的bean
*/
@Component
public class Hello {
public String getHello(){
return "Hello World";
}
}
- HelloWorldApp类,采用@ComponentScan注解
package com.spring.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
/**
* @ComponentScan用于寻找用component注解标注的bean
*/
@ComponentScan
public class HelloApp {
public static void main(String[] args) {
//1 通过注解创建上下文对象
ApplicationContext context =new AnnotationConfigApplicationContext(HelloApp.class);
//2 读取bean
Hello hello=context.getBean(Hello.class);
//3 运行
System.out.println(hello.getHello());
}
}
-
运行结果
图片.png
2、Student和Phone的例子改成用注解的方法实现
- Lombok插件的使用
-
打开Settings
图片.png
图片.png
下载Lombok
添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<scope>provided</scope>
</dependency>
- Phone类
package com.spring.annotation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
*采用注解和Lombok开发的Phone类
*/
@Component
@Data
public class Phone {
@Value("iphoneX")
private String brand;
@Value("2222")
private double price;
}
- Student类
package com.spring.annotation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class Student {
@Value("Tom")
private String name;
@Value("22")
private int age;
//使用@Autowired注入一个Phone类型的bean
@Autowired
private Phone phone;
}
- StudentApp类
package com.spring.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class StudentApp {
public static void main(String[] args) {
ApplicationContext context=new AnnotationConfigApplicationContext(StudentApp.class);
Student student=context.getBean(Student.class);
System.out.println(student);
}
}
运行结果
图片.png
3、添加控制台日志输出
log4j.properties
##日志输出的级别,以及配置记录方案
log4j.rootLogger=debug, stdout,file
###log4j.rootLogger=info, stdout,file
##设置日志记录到控制台的方式
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
##设置日志记录到文件的方式
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=logs/my_log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%
在resources中建log4j.properties 如图 将上面的代码复制进去
图片.png