• Spring3.0学习1.2(使用annotation)


    使用annotation

    首先 xml文件更改  新加xslt

    <?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="u" class="com.bjsxt.dao.impl.UserDAOImpl">
      </bean>
      <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 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;
        }
        
        @Autowired
        public void setUserDAO(@Qualifier("u") UserDAO userDAO) {
            this.userDAO = userDAO;
        }
        
    
        
        public void destroy() {
            System.out.println("destroy");
        }
    }

    @Autowired 默认是执行bytype的 如果出现多个 可以使用 @qualifier 指定

    Resourse

    引入j2ee的annotion-common包

    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
        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 />
      <bean id="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
      </bean>
      <bean id="u2" class="com.bjsxt.dao.impl.UserDAOImpl">
      </bean>
        
      <bean id="userService" class="com.bjsxt.service.UserService" >
          
      </bean>
      
    
    </beans>

    test

    package com.bjsxt.service;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.bjsxt.model.User;
    
    //Dependency Injection
    //Inverse of Control
    public class UserServiceTest {
    
        @Test
        public void testAdd() throws Exception {
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
            
            
            UserService service = (UserService)ctx.getBean("userService");
            service.add(new User());
            
            ctx.destroy();
            
        }
    
    }

  • 相关阅读:
    percona-toolkit
    美河在线
    http://planet.mysql.com/
    MySQL性能诊断与调优 转
    PDB CDB
    mysql安装三 linux源码安装mysql5.6.22
    Solaris10 下mysql5.5.12的安装
    c# 进程间通信
    C# 进程同步,通信
    有关DotNetBar设计样式和运行时的样式不一致的问题
  • 原文地址:https://www.cnblogs.com/frankzone/p/9457081.html
Copyright © 2020-2023  润新知