<?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-4.0.xsd"> <context:component-scan base-package="com.test.spring.beans"></context:component-scan> </beans>
package com.test.spring.beans; import org.springframework.beans.factory.annotation.Autowired; public class BaseService<T> { @Autowired protected BaseRepository repository; public void add() { System.out.println("add...."); System.out.println(repository); } }
package com.test.spring.beans; public class BaseRepository<T> { }
package com.test.spring.beans; import org.springframework.stereotype.Repository; @Repository public class UserRepository extends BaseRepository<User>{ }
package com.test.spring.beans; import org.springframework.stereotype.Service; @Service public class UserService extends BaseService<User>{ }
package com.test.spring.beans; public class User { }
package com.test.spring.beans; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext con=new ClassPathXmlApplicationContext("beans_fanxing.xml"); UserService u=(UserService)con.getBean("userService"); u.add(); } }
时间: 2024-10-29 19:08:48