【!!关键!!】Spring入门程序_09【Bean的常用装配(2)——基于Annotation(注解)装配】

目录

    • 小结
    • 目录结构
    • 代码
    • 输出

小结

这部分比较重要,考试必考!需要记忆理解的,很多都是注解的英文单词,以及写在哪里,以及xml文件的配置。。。。。
本部分,xml中注释的部分也可以单独拿出来,也可以实现与 不是注释 的那部分一样的效果,只不过注释的部分比较多,不注释的是精简版,使得代码不那么冗余。。。。。。。。。。。。。

目录结构

【!!关键!!】Spring入门程序_09【Bean的常用装配(2)——基于Annotation(注解)装配】_第1张图片

代码

UserDao接口

package com.itheima.annotation;
public interface UserDao {
     
    public void save();
}

UserDaoImpl类实现UserDao接口

新的内容 @Repository("userDao")

package com.itheima.annotation;
import org.springframework.stereotype.Repository;
@Repository("userDao") 
public class UserDaoImpl implements UserDao{
     
   public void save(){
     
	  System.out.println("userdao...save...");
   }
}

UserService接口

package com.itheima.annotation;
public interface UserService {
     
	public void save();
}

UserServiceImpl类实现 UserService接口

新的内容 @Service("userService") , @Resource(name="userDao")

package com.itheima.annotation;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service("userService") 
public class UserServiceImpl implements UserService{
     
	@Resource(name="userDao") 
	private UserDao userDao;
	public void save() {
     
         //调用userDao中的save()方法
		this.userDao.save();
		System.out.println("userservice....save...");
	}
	
	public void setUserDao(UserDao userDao) {
     
		this.userDao = userDao;
	}
}

控制器类

注意 @Controller("userController") , @Resource(name="userService")

package com.itheima.annotation;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
@Controller("userController")
public class UserController {
     
	@Resource(name="userService")
	private UserService userService;
	public void save(){
     
		this.userService.save();
		System.out.println("userController...save...");
	}
	
	public void setUserService(UserService userService) {
     
		this.userService = userService;
	}
}

beans6.xml


<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-4.3.xsd
      http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.3.xsd">
	 
	 
	 

	
	   
    
    
    
	<bean id="userDao" 
		class="com.itheima.annotation.UserDaoImpl" />
	<bean id="userService" 
	  class="com.itheima.annotation.UserServiceImpl" autowire="byName" />
	<bean id="userController" 
	  class="com.itheima.annotation.UserController" autowire="byName"/>
beans>

测试类

package com.itheima.annotation;
import org.springframework.context.ApplicationContext;
import 
   org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnotationAssembleTest {
     
	public static void main(String[] args) {
     
		
		String xmlPath = "com/itheima/annotation/beans6.xml";

		ApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext(xmlPath);
	
		UserController userController = 
		  (UserController) applicationContext.getBean("userController");
		
		userController.save();
	}
}  

输出

【!!关键!!】Spring入门程序_09【Bean的常用装配(2)——基于Annotation(注解)装配】_第2张图片

你可能感兴趣的:(ssm,spring,annotation,Repository,Controller,Service)