首页 > 代码库 > 注解模式2
注解模式2
IDao.java
package com.wh.IDao; public interface IDao { boolean insert(); }
OracleDaoImpl.java
package com.wh.dao; import org.springframework.stereotype.Repository; import com.wh.IDao.IDao; @Repository public class OracleDaoImpl implements IDao{ @Override public boolean insert() { return true; } }
MySQLDaoImpl.java
package com.wh.dao; import com.wh.IDao.IDao; public class MySQLDaoImpl implements IDao { @Override public boolean insert() { return true; } }
UserService.java
package com.wh.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.wh.dao.OracleDaoImpl; @Service public class UserService { @Resource(name="mySqlDaoImpl") //@Autowired只适合不是多态的实现,单个类(自动注解) private IDao dao;//若属性是接口,且有多个子类实现该接口时,建议使用@Resource(name="xxx")比较好,实现多态的效果 public boolean insert() { return dao.insert(); } public OracleDaoImpl getDao() { return dao; } public void setDao(OracleDaoImpl dao) { this.dao = dao; } }
UserController.java
package com.wh.control; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.wh.service.UserService; @Controller public class UserController { @Autowired private UserService us; public boolean requestInsert(){ return us.insert(); } public UserService getUs() { return us; } public void setUs(UserService us) { this.us = us; } }
TestMVC.java
package com.wh.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wh.bean.Student; import com.wh.control.UserController; public class TestMVC { @Test public void testUser2(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserController c =(UserController)ac.getBean("userController"); System.out.println(c); } }
applicationContext.xml
package com.wh.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wh.bean.Student; import com.wh.control.UserController; public class TestMVC { @Test public void testUser2(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserController c =(UserController)ac.getBean("userController"); System.out.println(c); } }
注解模式2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。