美文网首页
4、以注解方式开发bean

4、以注解方式开发bean

作者: m小萌同学 | 来源:发表于2019-03-01 10:31 被阅读0次

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());
    }
}
  • 运行结果


    运行结果.jpg

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

  • Lombok插件的使用
    打开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开发的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注入一个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);
    }
}
  • 运行结果


    运行结果.jpg

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%n

4、深入学习Lombok

Lombok注解

相关文章

网友评论

      本文标题:4、以注解方式开发bean

      本文链接:https://www.haomeiwen.com/subject/kfiruqtx.html