Spring Boot的注解配置和java配置

注解配置

声明bean

  • @Component组件
  • @Service业务逻辑层使用
  • @Repository数据访问层(dao)使用
  • @Controller展现层使用
    注入bean
  • @Autowired

编写功能bean
其中@Component、@Service、@Repository和@Controller相同,可选择

package com.example.demoboot.Di;

import org.springframework.stereotype.Service;

@Service//标记为需要装配的类,为spring管理的一个bean
public class demofuncService {
    public String demofun(String st){
        return "demofun!--"+st+"--!";
    }

}

配置类
此处引用记录一下:
@Configuration注解的配置类有如下要求:
@Configuration不可以是final类型;
@Configuration不可以是匿名类;
嵌套的configuration必须是静态类。
一、用@Configuration加载spring
1.1、@Configuration配置spring并启动spring容器
1.2、@Configuration启动容器+@Bean注册Bean
1.3、@Configuration启动容器+@Component注册Bean
1.4、使用 AnnotationConfigApplicationContext 注册 AppContext 类的两种方法
1.5、配置Web应用程序(web.xml中配置AnnotationConfigApplicationContext)

@Configuation加载Spring方法:
@Configuration配置spring并启动spring容器
@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的,作用为: 配置spring容器(应用上下文)

@Configuation总结:
@Configuation等价于
@Bean等价于
@ComponentScan等价于<context:component-scan base-package=”com.dxz.demo”/>
原文:https://blog.csdn.net/BinshaoNo_1/article/details/85005935

package com.example.demoboot.Di;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.example.demoboot.Di")
public class Diconfig {
}

使用功能类的Bean
使用@Service注解声明当前类似spring管理的一个bean
使用@Autowired将demofuncService类实体bean注入到UseFuncService

package com.example.demoboot.Di;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UseFuncService {
    @Autowired
    demofuncService funcService;

    public String DamoFun(String node){
        return funcService.demofun(node);
    }
}

执行

package com.example.demoboot;

import com.example.demoboot.Di.Diconfig;
import com.example.demoboot.Di.UseFuncService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@SpringBootApplication
public class DemoBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoBootApplication.class, args);

        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(Diconfig.class);
        UseFuncService usefs = context.getBean(UseFuncService.class);
        System.out.println(usefs.DamoFun("ok"));
        context.close();
    }

}

最后结果:
结果
java配置
java配置是通过@Configuration和@Bean来实现。

package com.example.demoboot.Di;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Javaconfig {
    @Bean
    public demofuncService demofuncService(){
        return new demofuncService();
    }

    @Bean//注入bean时直接调用demofuncService()
    public UseFuncService useFuncService(){
        UseFuncService useFuncService = new UseFuncService();
        useFuncService.setfuncService(demofuncService());
        return useFuncService;
    }

    @Bean//只要存在某个Bean都可以直接在另外一个Bean的声明方法参数写入
    public UseFuncService useFuncService(demofuncService demofuncService){
        UseFuncService useFuncService = new UseFuncService();
        useFuncService.setfuncService(demofuncService());
        return useFuncService;
    }
}

package com.example.demoboot.Di;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//@Service
public class UseFuncService {
    //@Autowired
    demofuncService funcService;

    public void setfuncService(demofuncService demofuncService){
        this.funcService = demofuncService;
    }

    public String DamoFun(String node){
        return funcService.demofun(node);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值