Spring代码实例系列-06:通过注解@Component、@Controller、@Service和@Repository定义bean,@Autowire装配bean

超级通道: Spring代码实例系列-绪论

Spring中有三种定义Bean的方式:
1. *.xml//
2. @Configuration/@Bean/@Import
3. @Component/@Controller/@Service/@Repository

之前的实例中,已经对前两种方式进行了记录,本章节记录@Component/@Controller/@Service/@Repository的方。

0.程序目录

project
    \---src
        \---main
            \---java
            |   \---pers
            |       \---hanchao
            |           \---component
            |               \---App.java
            |               \---DemoComponent.java
            |               \---DemoController.java
            |               \---DemoService.java
            |               \---DemoRepository.java
            \---resources
                \---spring-config
                    \---spring-component.xml

1.App.java

package pers.hanchao.component;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * <p>通过@Component、@Controller、@Service、@Repository注解进行bean的定义,通过@Autowired进行bean的自动装配</p>
 * @author hanchao 2018/1/7 19:51
 **/
public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config/spring-component.xml");
        //@Component
        DemoComponent demoComponent = (DemoComponent) context.getBean("demoComponent");
        demoComponent.showMessage();
        //@Controller、@Service、@Repository+@Autowired构成的三层注解
        DemoController demoController = (DemoController) context.getBean("demoController");
        demoController.showMessage();
    }
}

2.spring-component.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解-->
    <context:annotation-config />
    <!--开启@Componet扫描,@Controller、@Service、@Repository都属于@Component注解-->
    <context:component-scan base-package="pers.hanchao.*"/>
</beans>

3.DemoComponent.java

package pers.hanchao.component;

import org.springframework.stereotype.Component;

@Component
public class DemoComponent {
    public void showMessage(){
        System.out.println("@Component is success!");
    }
}

4.DemoController.java

package pers.hanchao.component;

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

@Controller
public class DemoController {
    @Autowired
    private DemoService demoService;

    public void showMessage(){
        this.demoService.showMessage();
        System.out.println("@Controller is success!");
    }
}

5.DemoService.java

package pers.hanchao.component;

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

@Service
public class DemoService {
    @Autowired
    private DemoRepository demoRepository;

    public void showMessage() {
        this.demoRepository.showMessage();
        System.out.println("@Service is success!");
    }
}

6.DemoRepository.java

package pers.hanchao.component;

import org.springframework.stereotype.Repository;

@Repository
public class DemoRepository {
    public void showMessage() {
        System.out.println("@Repository is success!");
    }
}

7.result

@Component is success!
@Repository is success!
@Service is success!
@Controller is success!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值