注解方式开发bean

2019-03-01  本文已影响0人  忆曦雪_

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;

//用于寻找用@componnent注解标注的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的例子用注解的方法实现

<!--lombox依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.10</version>
        </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("荣耀v20")
    private  String brand;

    @Value("2999.0")
    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;

@Data
@Component
public class Student {
    @Value("Tom")
    private  String name;

    @Value("20")
    private double 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);

    }
}
##日志输出的级别,以及配置记录方案
log4j.rootLogger=info,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

3、深入学习Lombok

Lombok原理分析与功能实现
常用的注解:

上一篇下一篇

猜你喜欢

热点阅读