首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Spring入门

>@Required

>@Autowired

示例:

spring-injection.xml配置:

InjectionDAO层:

InjectionDAOImpl实现:

package com.imooc.beanannotation.injection.dao;import org.springframework.stereotype.Repository;@Repositorypublic class InjectionDAOImpl implements InjectionDAO {public void save(String arg) {//模拟数据库保存操作System.out.println("保存数据:" + arg);}}

InjectionService层:

InjectionServiceImpl实现:

package com.imooc.beanannotation.injection.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.imooc.beanannotation.injection.dao.InjectionDAO;@Servicepublic class InjectionServiceImpl implements InjectionService {//@Autowiredprivate InjectionDAO injectionDAO;@Autowiredpublic InjectionServiceImpl(InjectionDAO injectionDAO) {this.injectionDAO = injectionDAO;}//@Autowiredpublic void setInjectionDAO(InjectionDAO injectionDAO) {this.injectionDAO = injectionDAO;}public void save(String arg) {//模拟业务操作System.out.println("Service接收参数:" + arg);arg = arg + ":" + this.hashCode();injectionDAO.save(arg);}}

测试类:

package com.imooc.test.beanannotation;import org.junit.Test;import org.junit.runner.RunWith;import org.junit.runners.BlockJUnit4ClassRunner;import com.imooc.beanannotation.injection.service.InjectionService;import com.imooc.beanannotation.multibean.BeanInvoker;import com.imooc.test.base.UnitTestBase;@RunWith(BlockJUnit4ClassRunner.class)public class TestInjection extends UnitTestBase {public TestInjection() {super("classpath:spring-beanannotation.xml");}@Testpublic void testAutowired() {InjectionService service = super.getBean("injectionServiceImpl");service.save("This is autowired.");}@Testpublic void testMultiBean() {BeanInvoker invoker = super.getBean("beanInvoker");invoker.say();}}

测试结果:

七月 10, 2018 11:06:07 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5d22bbb7: startup date [Tue Jul 10 23:06:07 GMT+08:00 2018]; root of context hierarchy七月 10, 2018 11:06:07 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [spring-beanannotation.xml]七月 10, 2018 11:06:08 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [config.xml]七月 10, 2018 11:06:08 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties信息: Loading properties file from class path resource [config.properties]七月 10, 2018 11:06:08 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiringJsrServie init.Service接收参数:This is autowired.保存数据:This is autowired.:581318631七月 10, 2018 11:06:08 下午 org.springframework.context.support.AbstractApplicationContext doClose信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5d22bbb7: startup date [Tue Jul 10 23:06:07 GMT+08:00 2018]; root of context hierarchyJsrServie destroy.七月 10, 2018 11:06:08 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@769f71a9: startup date [Tue Jul 10 23:06:08 GMT+08:00 2018]; root of context hierarchy七月 10, 2018 11:06:08 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [spring-beanannotation.xml]七月 10, 2018 11:06:08 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [config.xml]七月 10, 2018 11:06:08 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties信息: Loading properties file from class path resource [config.properties]七月 10, 2018 11:06:08 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor 信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiringJsrServie init.list...com.imooc.beanannotation.multibean.BeanImplTwocom.imooc.beanannotation.multibean.BeanImplOnemap...beanImplOne com.imooc.beanannotation.multibean.BeanImplOnebeanImplTwo com.imooc.beanannotation.multibean.BeanImplTwocom.imooc.beanannotation.multibean.BeanImplTwo七月 10, 2018 11:06:08 下午 org.springframework.context.support.AbstractApplicationContext doClose信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@769f71a9: startup date [Tue Jul 10 23:06:08 GMT+08:00 2018]; root of context hierarchyJsrServie destroy.

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180718G0BNF600?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券