sping

UserDao类  get,set,toString方法

辅助Clazz

public class Clazz {
    private Integer cid;
    private String cname;

    @Override
    public String toString() {
        return "Clazz{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                '}';
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }
}
public class UserDao {
    private Integer age;
    private String name;
    private Clazz clazz;//注入对象
    private List hoppy;//注入集合
    private Set set;//注入set
    private Integer[] sex;
    private Map map;//注入map

    @Override
    public String toString() {
        return "UserDao{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", clazz=" + clazz +
                ", hoppy=" + hoppy +
                ", set=" + set +
                ", sex=" + Arrays.toString(sex) +
                ", map=" + map +
                '}';
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Integer[] getSex() {
        return sex;
    }

    public void setSex(Integer[] sex) {
        this.sex = sex;
    }

    public Set getSet() {
        return set;
    }

    public void setSet(Set set) {
        this.set = set;
    }

    public List getHoppy() {
        return hoppy;
    }

    public void setHoppy(List hoppy) {
        this.hoppy = hoppy;
    }

    public Clazz getClazz() {
        return clazz;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

application配置


    
    
    

    
        
            抽烟
            喝酒
            打麻将
        
    
    
    
        
            抽烟1
            喝酒1
            打麻将1
        
    
    
    
        
            0
            1
            2
        
    
    
    
        
            
            
            
        
    


    
        
        
    

Demo测试

ApplicationContext context = new ClassPathXmlApplicationContext("aplication.xml");
UserDao bean =(UserDao) context.getBean("userDao");
System.out.println(bean.toString());

 自动注入

public class UserController {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void fun(){
        userDao.show();
    }
}

 bean.xml


    
        
    

    

    

  

 

使用注解完成IOC和DI功能

 application.xml

 各个接口实现类写Controller和Respository即可

 @Controller+@Respository+@Service+@Component
没有区别: 他们的底层都是用的@Component
实际意思就在于区分层的。
@Controller控制层
@Respository持久化层
@Service 业务层
@Component 最底层的bean注解。

 UserController

@Controller
//表示控制层注解,如何没有设置bean的id 默认为类的名称首字母小写
// 相当于在spring配置文件钟
public class UserController {
    @Autowired
    @Qualifier(value = "userDaoImpl01")//
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
    public void fun(){
        userDao.show();
    }
}

demo

 

public void show(){
    ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
    UserController bean =(UserController) context.getBean("userController");
    bean.fun();
}

你可能感兴趣的:(java,开发语言)