如题,明天来跟新……,怕自己忘了做做笔记,感觉这个在开发中比较有价值和实际应用
在父类或者是接口中定义好以有的关系,各自的子类继承这种关系,以一个简单的例子来说明:
首先建立目录如下:
可以比较清楚看建立的层次目录建立的各个文件及之间的关系,下面我们来看看类的内容:
User.java
package com.tianjun.h.entity; public class User { }
BaseRespository.java
package com.tianjun.h.repository; public class BaseRespository<T> { }
UserRepositoryImpl.java
package com.tianjun.h.repository.impl; import org.springframework.stereotype.Repository; import com.tianjun.h.entity.User; import com.tianjun.h.repository.BaseRespository; @Repository public class UserRepositoryImpl extends BaseRespository<User>{ }
BaseService.java
package com.tianjun.h.service; import org.springframework.beans.factory.annotation.Autowired; import com.tianjun.h.repository.BaseRespository; public class BaseService<T> { @Autowired private BaseRespository<T> baseRespositroy; public void add(){ System.out.println("nihao,wohao !dajiahao!"); System.out.println(baseRespositroy); } }
UserServieImpl.java
package com.tianjun.h.service.Impl; import org.springframework.stereotype.Service; import com.tianjun.h.entity.User; import com.tianjun.h.service.BaseService; @Service("userService") public class UserServieImpl extends BaseService<User> { }
最后测试下:test.java
package com.tianjun.h; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tianjun.h.service.BaseService; public class test{ @Test public static void main(String[] args){ String path = "D:\\tianWorkPlace\\h\\beans.xml"; ApplicationContext atx = new ClassPathXmlApplicationContext("beans.xml"); BaseService userServie = (BaseService) atx.getBean("userService"); userServie.add(); } }
beans.xml中加入一句扫描包的配置就可以 了!
<context:component-scan base-package="com.tianjun.h"></context:component-scan>