4、以注解方式开发bean

2019-03-01  本文已影响0人  m小萌同学

1、HelloWorld的例子改成用注解的方法实现

package com.spring.annotation;

import org.springframework.stereotype.Component;

/**
 * 采用注解开发的bean
 * @Component用于类级别注解,标注本类为一个可被Spring容器托管的bean
 */
@Component
public class Hello {
    public String getHello(){
        return "Hello World";
    }
}
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());
    }
}

2、Student和Phone的例子改成用注解的方法实现

 <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>
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("6666.66")
    private double price;
}
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注入一个Phone类型的bean
    @Autowired
    private Phone phone;
}
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);
    }
}

3、添加控制台日志输出

##日志输出的级别,以及配置记录方案
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%n

4、深入学习Lombok

Lombok注解

上一篇 下一篇

猜你喜欢

热点阅读