1、將上節(jié)課的HelloWorld項目用注解的方式實現(xiàn)
- Student類采用@Component注解
package com.spring.annotation;
import org.springframework.stereotype.Component;
/**
* 采用注解開發(fā)bean
* @Component 用于類級別注解,標(biāo)注本類為一個可被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;
//用于尋找用@componnent注解標(biāo)注的bean
@ComponentScan
public class HelloApp {
public static void main(String[] args) {
// 1 通過注解創(chuàng)建上下文對象
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é)果
2、Student和Phone的例子用注解的方法實現(xiàn)
- Lombok插件的使用
-
1.Settings->plugins,搜索Lombok,安裝,重啟IDEA
settings
搜索并安裝 - 注:圖片中此前已安裝過lombok,所以顯示有所不同,安裝完需重啟idea
- 2.添加依賴
-
<!--lombox依賴-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
- 版本號一般是自動生成的,如果不正確視情況修改
- Phone類
package com.spring.annotation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//采用注解和Lombok開發(fā)的Phone類
@Component
@Data
public class Phone {
@Value("榮耀v20")
private String brand;
@Value("2999.0")
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;
@Data
@Component
public class Student {
@Value("Tom")
private String name;
@Value("20")
private double 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);
}
}
- 添加日志文件log4j.properties
##日志輸出的級別,以及配置記錄方案
log4j.rootLogger=info,stdout,file
###log4j.rootLogger=info, stdout,file
##設(shè)置日志記錄到控制臺的方式
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
-
運(yùn)行結(jié)果
運(yùn)行結(jié)果
3、深入學(xué)習(xí)Lombok
Lombok原理分析與功能實現(xiàn)
常用的注解:
@Data:注解在類上,將類提供的所有屬性都添加get、set方法,并添加、equals、canEquals、hashCode、toString方法
@Setter:注解在類上,為所有屬性添加set方法、注解在屬性上為該屬性提供set方法
@Getter:注解在類上,為所有的屬性添加get方法、注解在屬性上為該屬性提供get方法
@NotNull:在參數(shù)中使用時,如果調(diào)用時傳了null值,就會拋出空指針異常
@Synchronized 用于方法,可以鎖定指定的對象,如果不指定,則默認(rèn)創(chuàng)建一個對象鎖定
@Log作用于類,創(chuàng)建一個log屬性
@Builder:使用builder模式創(chuàng)建對象
@NoArgsConstructor:創(chuàng)建一個無參構(gòu)造函數(shù)
@AllArgsConstructor:創(chuàng)建一個全參構(gòu)造函數(shù)
@ToStirng:創(chuàng)建一個toString方法
@Accessors(chain = true)使用鏈?zhǔn)皆O(shè)置屬性,set方法返回的是this對象。
(鏈?zhǔn)皆O(shè)置屬性舉例:
- image
@RequiredArgsConstructor:創(chuàng)建對象
@UtilityClass:工具類
@ExtensionMethod:設(shè)置父類
@FieldDefaults:設(shè)置屬性的使用范圍,如private、public等,也可以設(shè)置屬性是否被final修飾。
@Cleanup: 關(guān)閉流、連接點。
@EqualsAndHashCode:重寫equals和hashcode方法。
@toString:創(chuàng)建toString方法
更多的請參見:https://projectlombok.org/features/index.html



