标签:set get span print 测试 size vax junit location
#自动装配的小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 基础很重要!
标签:set get span print 测试 size vax junit location
原文地址:https://www.cnblogs.com/shijinglu2018/p/10360461.html