4、以注解方式開(kāi)發(fā)bean

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

4、深入學(xué)習(xí)Lombok

Lombok注解

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容