zoukankan      html  css  js  c++  java
  • Spring再接触 Annotation part2

    resource

    resource

    beans.xml

    <?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-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">
        <context:annotation-config />
      <bean id="u2" class="com.bjsxt.dao.impl.UserDAOImpl">
      </bean>
        
      <bean id="userService" class="com.bjsxt.service.UserService" >
          
      </bean>
      
    
    </beans>

    Userservice.java

    package com.bjsxt.service;
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    
    
    public class UserService {
        
        private UserDAO userDAO;  
        
        public void init() {
            System.out.println("init");
        }
        
        public void add(User user) {
            userDAO.save(user);
        }
        public UserDAO getUserDAO() {
            return userDAO;
        }
        
        @Resource   //默认按名字 找不到UserDao的话,名称找不到,按类型
        public void setUserDAO( UserDAO userDAO) {
            this.userDAO = userDAO;
        }
        
    
        
        public void destroy() {
            System.out.println("destroy");
        }
    }

    test

        @Test
        public void testAdd() throws Exception {
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
            
            
            UserService service = (UserService)ctx.getBean("userService");
            service.add(new User());
            
            ctx.destroy();
            
        }

    Component

    userDaoimpl

    package com.bjsxt.dao.impl;
    
    import org.springframework.stereotype.Component;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    @Component("u") 
    public class UserDAOImpl implements UserDAO {
    
        public void save(User user) {
            //Hibernate
            //JDBC
            //XML
            //NetWork
            System.out.println("user saved!");
        }
    
    }

    Userservice

    package com.bjsxt.service;
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    
    @Component("userService")
    public class UserService {
        
        private UserDAO userDAO;  
        
        public void init() {
            System.out.println("init");
        }
        
        public void add(User user) {
            userDAO.save(user);
        }
        public UserDAO getUserDAO() {
            return userDAO;
        }
        
        @Resource(name="u")
        public void setUserDAO( UserDAO userDAO) {
            this.userDAO = userDAO;
        }
        
    
        
        public void destroy() {
            System.out.println("destroy");
        }
    }

    beans.xml

    <?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-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd">
        <context:annotation-config />
        <context:component-scan base-package="com.bjsxt"/>          //挨着盘在这下面的包找
     
        
     
    
    </beans>

    test

    @Test 
        public void testAdd() throws Exception {
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
            
            UserService service = (UserService)ctx.getBean("userService");
            service.add(new User());
            
            ctx.destroy();
            
        }

    最后一个 PostConstruct 在方法之前  Predestroy 在小销毁之前

    package com.bjsxt.service;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import javax.annotation.Resource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    
    
    @Component("userService")
    public class UserService {
        
        private UserDAO userDAO;  
        
        @PostConstruct
        public void init() {
            System.out.println("init");
        }
        
        public void add(User user) {
            userDAO.save(user);
        }
        public UserDAO getUserDAO() {
            return userDAO;
        }
        
        @Resource(name="u")
        public void setUserDAO( UserDAO userDAO) {
            this.userDAO = userDAO;
        }
        
    
        @PreDestroy
        public void destroy() {
            System.out.println("destroy");
        }
    }
  • 相关阅读:
    spring读取配置文件内容并自动注入
    xshell免费下载安装使用
    cas sso原理
    sql两列相除,保留n位小数
    mysql 报zone什么的错误
    mysql union出错: "Every derived table must have its own alias"
    mysql jdbc操作
    sql 对某列取值进行if判断
    Python深入:02浅拷贝深拷贝
    Python基础:22__slots__类属性
  • 原文地址:https://www.cnblogs.com/frankzone/p/9613593.html
Copyright © 2011-2022 走看看