Spring基于的注解自动装配和依赖注入(***)
Posted 贰零一八
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring基于的注解自动装配和依赖注入(***)相关的知识,希望对你有一定的参考价值。
#自动装配的小Demo:
package com.gyf.annotation; public interface UserDao { public void save(); }
package com.gyf.annotation; import org.springframework.stereotype.Repository; @Repository("userDao") public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("UserDao接口的实现类的save方法的实现..."); } }
package com.gyf.annotation; public interface UserService { void save(); } package com.gyf.annotation; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service("userService") public class UserServiceImpl implements UserService { @Resource(name = "userDao") private UserDao userDao; @Override public void save() { this.userDao.save(); System.out.println("userservice...save..."); } }
#配置文件:
<?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:component-scan base-package="com.gyf.annotation"/> </beans>
package com.gyf.test; import com.gyf.annotation.UserController; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class test { public static void main(String[] args) { String xmlPath="beans6.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath); UserController userController=(UserController)ac.getBean("userController"); userController.save(); } }
运行结果:userController save...
----------------------------------------------------------------------------------------------------------------------------------------------
#依赖注入
package com.gyf.ioc; public interface UserDao { void say(); } package com.gyf.ioc; public class UserDaoImpl implements UserDao { @Override public void say() { System.out.println("userDao say hello world"); } } package com.gyf.ioc; public interface UserService { void say(); } package com.gyf.ioc; public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void say() { this.userDao.say(); System.out.println("userService say hello world"); } }
#配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <bean id="userDao" class="com.gyf.ioc.UserDaoImpl"/> <bean id="userService" class="com.gyf.ioc.UserServiceImpl"> <property name="userDao" ref="userDao"/> </bean> </beans>
#测试:
package com.gyf.test; import com.gyf.ioc.UserDao; import com.gyf.ioc.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class test { @Test public void test1(){ ApplicationContext ac=new ClassPathXmlApplicationContext("beansIOC.xml"); UserDao userDao=(UserDao)ac.getBean("userDao"); userDao.say(); UserService userService=(UserService)ac.getBean("userService"); userService.say(); } }
学会重复!
多练习,Spring 基础很重要!
以上是关于Spring基于的注解自动装配和依赖注入(***)的主要内容,如果未能解决你的问题,请参考以下文章
Spring 依赖注入(DI)详解 [Spring][依赖注入的 6 种实现方式][setter注入][构造器注入][注解注入][自动装配注入][静态工厂注入][实例工厂注入]
框架 day36 Spring3 入门,DI依赖注入,装配bean基于xml/注解, 整合Junit4,配置约束自动提示