//接口
package com.hanqi.entity;
public interface ITest {
void test();
}
package com.hanqi.entity;
import org.springframework.stereotype.Component;
@Component
public class Test2 implements ITest {
@Override
public void test() {
// TODO 自动生成的方法存根
System.out.println("实现类是4");
}
}
package com.hanqi.entity;
import org.springframework.stereotype.Component;
@Component
public class Tset1 implements ITest {
@Override
public void test() {
// TODO 自动生成的方法存根
System.out.println("实现了1");
}
}
package com.hanqi.entity;
import org.springframework.stereotype.Repository;
@Repository(value="user")
public class User {
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.hanqi.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.hanqi.entity.ITest;
import com.hanqi.entity.User;
@Service(value="us")
public class UserService {
@Autowired
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void add()
{
System.out.println("userservice aitowired"+user);
test.test();
}
@Autowired
@Qualifier("test2")
private ITest test;
public ITest getTest() {
return test;
}
public void setTest(ITest test) {
this.test = test;
}
}
package com.hanqi.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hanqi.entity.User;
import com.hanqi.service.UserService;
public class testm {
public static void main(String[] args) {
//加载容器
ApplicationContext ac=new ClassPathXmlApplicationContext("ccc.xml");
test1 t1=(test1)ac.getBean("t1");
t1.Show();
User u1=ac.getBean(User.class);
System.out.println(u1);
UserService us=(UserService)ac.getBean("us");
us.add();
}
}
package com.hanqi.test;
import org.springframework.stereotype.Component;
@Component("t1")
public class test1 {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void Show()
{
System.out.println("sa撒旦下"+name+age);
}
}
<?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"
xmlns:util="http://www.springframework.org/schema/util"
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.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 启动扫描器 -->
<context:component-scan base-package="com.hanqi"></context:component-scan>
</beans>