Spring3的IOC的annotation学习笔记
阅读原文时间:2023年07月09日阅读:1

以下记录了一个小小的Spring3的annotation的应用笔记。

文件列表:

UserService-interface

UserDao-interface

UserServiceImpl-UserService接口的实现

UserDaoImpl-UserDao接口的实现

User-实体类

package com.niewj.model;

public class User {

    private String name;

    public String getName() {
        return name;
    }

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


package com.niewj.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.niewj.dao.LogDao;
import com.niewj.dao.UserDao;
import com.niewj.model.User;

@Service("userServiceImplementation")
// @Component/@Repository/@Controller/@Service
@Scope("singleton")
// @Scope("prototype")
public class UserServiceImpl implements UserService {

//  也可以此处声明,也可以在setter处声明。
/*    @Autowired
    @Qualifier("userDaoImplementation")*/
    //@Resource(name="userDaoImplementation")// 不指定的话,他会找setter方法,最后可以会退到,找byType匹配。
    private UserDao uuuserDao;

    
    private LogDao logDao;

    public UserDao getUuuserDao() {
        return uuuserDao;
    }

    /*
    @Autowired// (required=false)
    @Qualifier("userDaoImplementation")// id/name
     */
    @Resource(name="userDaoImplementation")
    public void setUuuserDao(UserDao uuuserDao) {
        this.uuuserDao = uuuserDao;
    }

    public LogDao getLogDao() {
        return logDao;
    }

    @Autowired
    public void setLogDao(LogDao logDao) {
        this.logDao = logDao;
    }

    @Override
    public boolean regist(User user) {
        logDao.log();
        return uuuserDao.save(user);
    }
}



package com.niewj.dao;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Repository;

import com.niewj.model.User;

/* @Repository/@Controller/@Service都属于@Component,只是为了更明显的标注业务层、控制层、还是Dao层 */

@Repository("userDaoImplementation")
public class UserDaoImpl implements UserDao {

&nbsp;&nbsp; &nbsp;@PostConstruct// 相当于<bean init-method="init">
&nbsp;&nbsp; &nbsp;public void init(){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.err.println("实例化DAO后,会马上调用此方法。");
&nbsp;&nbsp; &nbsp;}

&nbsp;&nbsp; &nbsp;@PreDestroy//相当于<bean destroy-method="destroy">
&nbsp;&nbsp; &nbsp;public void destroy(){
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.err.println("销毁DAO之前,会执行此方法。");
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;@Override
&nbsp;&nbsp; &nbsp;public boolean save(User user) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.err.println("新增用户:" +user.getName());
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true;
&nbsp;&nbsp; &nbsp;}
}



package com.niewj.dao;

import org.springframework.stereotype.Repository;

@Repository("logDao")
public class LogDaoImpl implements LogDao {

    @Override
    public void log() {
        System.out.println("Logging......记录在日志............OK");
    }
}


package com.niewj;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.niewj.model.User;
import com.niewj.service.UserService;
import com.niewj.service.UserServiceImpl;

public class AnnotationTest {
&nbsp;&nbsp; &nbsp;@Test
&nbsp;&nbsp; &nbsp;public void testIocAnnotation() {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;UserService loginService = ac.getBean("userServiceImplementation", UserServiceImpl.class);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;User user = new User();
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;user.setName("dotjar");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;loginService.regist(user);
&nbsp;&nbsp; &nbsp;}
}


http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<import resource="beans.xml" />


http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    
    
    2024 .

浙ICP备15029886号