1、HelloWorld的例子改成用注解的方法實(shí)現(xiàn)
- Student類采用@Component注解
package com.spring.annotation;
import org.springframework.stereotype.Component;
/**
* 采用注解開(kāi)發(fā)的bean
* @Component用于類級(jí)別注解,標(biāo)注本類為一個(gè)可被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注解標(biāo)注的bean
*/
@ComponentScan
public class HelloApp {
public static void main(String[] args) {
//1 通過(guò)注解創(chuàng)建上下文對(duì)象
ApplicationContext context =new AnnotationConfigApplicationContext(HelloApp.class);
//2 讀取bean
Hello hello=context.getBean(Hello.class);
//3 運(yùn)行
System.out.println(hello.getHello());
}
}
-
運(yùn)行結(jié)果
運(yùn)行結(jié)果.jpg
2、Student和Phone的例子改成用注解的方法實(shí)現(xiàn)
-
Lombok插件的使用
打開(kāi)Settings
Settings.jpg
選擇Plugins,搜索Lombok,安裝,重啟IDEA
Plugins.jpg
添加依賴
<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開(kāi)發(fā)的Phone類
*/
@Component
@Data
public class Phone {
@Value("iPhoneX")
private String brand;
@Value("6666.66")
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("20")
private int age;
//使用@Autowired注入一個(gè)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);
}
}
-
運(yùn)行結(jié)果
運(yùn)行結(jié)果.jpg
3、添加控制臺(tái)日志輸出
- log4j.properties
##日志輸出的級(jí)別,以及配置記錄方案
log4j.rootLogger=debug, stdout,file
###log4j.rootLogger=info, stdout,file
##設(shè)置日志記錄到控制臺(tái)的方式
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
##設(shè)置日志記錄到文件的方式
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%n



