Day20——@Autowired组件装配

一. 知识储备

1.1 需求

Controller组件中往往需要用到Service组件的实例,Service组件中往往需要用到Repository组件的实例。Spring可以通过注解的方式帮我们实现属性的装配。即实现下面功能

//在Controller组件中实现
private Service service = new UserServiceImpl();

//在Service组件中实现
private UserDao dao = new UserDaoJdbcImpl();

1.2 实现依据

在扫描指定的包时,<context:component-scan>元素会自动注册一个bean的后置处理器:AutowiredAnnotationBeanPostProcessor的实例。该后置处理器可以自动装配标志了@Autowired、@Resource、@Inject的注解的属性。

1.3 关于@Autowired注解

  1. 根据类型自动装配
  2. 构造器、普通字段(即使是非public)、有参数的方法都可以用@Autowired注解
  3. 默认情况下,所有使用@Autowired的属性都要被设置。当spring找不到匹配的bean装配时会发生异常

二. 例子

使用注解进行Controller-Service-Dao组件之间的调用。

spring-annotation.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-4.0.xsd">

    <context:component-scan base-package="com.atguigu.annotation" >
    </context:component-scan>
</beans>

UserController.java

package com.atguigu.annotation.controller;

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

import com.atguigu.annotation.service.UserService;

/**
 *@Controller注解的功能就相当于在xml中进行如下的配置:
 *<bean id="userController" class="com.atguigu.annotation.controller.UserController"></bean>
 * 默认的情况下,通过注解的方式将当前的类管理到Spring IOC容器中,使用的id值就是类名首字母小写
 * 也可以具体指定id值,通过注解中的value属性指定
 * @Controller(value="id值")或者@Controller("id值")
 *  
 * @author user
 *
 */

@Controller
public class UserController {
	   
	   @Autowired
       private UserService userService;
	   
	   public void regist(){
		     userService.doRegist();
	   }
	   
	   
}

UserService.java

package com.atguigu.annotation.service;

public interface UserService {

	void doRegist();

}

UserServiceImpl.java

package com.atguigu.annotation.service;

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

import com.atguigu.annotation.dao.UserDao;

@Service
public class UserServiceImpl implements UserService {
       
	   @Autowired
	   private UserDao userDao;

	@Override
	public void doRegist() {
		// TODO Auto-generated method stub
		
		userDao.insertUser();
	}
}

UserDao.java

package com.atguigu.annotation.dao;

public interface UserDao {

	void insertUser();

}

UserDaoJdbcImpl.java

package com.atguigu.annotation.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoJdbcImpl implements UserDao{

	@Override
	public void insertUser() {
		// TODO Auto-generated method stub
		
		System.out.println("JDBC insert User...");
		
	}

}

Main.java

package com.atguigu.annotation.test;

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

import com.atguigu.annotation.controller.UserController;
import com.atguigu.annotation.dao.UserDao;
import com.atguigu.annotation.service.UserService;

public class Main {
       public static void main(String[] args) {
		  ApplicationContext context = 
				  new ClassPathXmlApplicationContext("spring-annotation.xml");
		  //UserController
		  UserController uc = context.getBean("userController", UserController.class);
		  System.out.println("userController:" + uc);
		  //UserService
		  UserService us = context.getBean("userServiceImpl", UserService.class);
		  System.out.println("userService:" + us);
		  //UserDao
		  UserDao ud = context.getBean("userDaoJdbcImpl", UserDao.class);
		  System.out.println("userDaoJdbcImpl:" + ud);
		  
		  uc.regist();
	}
}

注意: 如果报错了,99%都是组件还没有装配好,需要检查service对象、dao对象是否已经使用@Autowired

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值