http://www.cnblogs.com/guzhulangzi/articles/6796317.html  实现功能类似这个  只不过这次用的是注解

1.

1.扫描含有注解的类

<context:component-scan base-package="....">

2.常见的注解

@Component  组件,任意bean

WEB

@Controller  web

@Service service

@Repository dao

注入  --> 字段或setter方法

普通值:@Value

引用值:

类型:@Autowired

名称1@Autowired  @Qualifier("名称")

名称2@Resource("名称")

作用域:@Scope("prototype")

生命周期:

初始化:@PostConstruct

销毁方法:@PreDestroy

 

 

1测试类

package com.spring.demo1.TestXmlSettervalue;

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

public class TestIoc {


// public TestIoc name() {
//
// }
//

@Test
public void Test()
{
String xmlPahtString="com/spring/demo1/TestXmlSettervalue/SpringConfig.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPahtString);
StudentService userService = (StudentService) applicationContext.getBean("StudentServiceid");
userService.add();
}
}

 

2.Dao接口

package com.spring.demo1.TestXmlSettervalue;

public interface StudentDao {
public void save();
}

 

3.Dao接口实现类

package com.spring.demo1.TestXmlSettervalue;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Repository  //注解类

//@Repository("studentdao")  提供名字  bean  id=“”

public class StudentDaoImp implements StudentDao {

@Override
public void save() {
// TODO Auto-generated method stub
System.out.print("11111111");
}

}

 

4.Service接口

package com.spring.demo1.TestXmlSettervalue;

public interface StudentService {
public void add();
}

5.service接口实现类别

package com.spring.demo1.TestXmlSettervalue;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("StudentServiceid")
public class StudentServiceImp implements StudentService {

@Autowired   //自动根据类型注入
private StudentDao studentDao;


//@Autowired
// @Qualifier("studentdao")   //根据名字注入
// public void setStudentDao(StudentDao studentDao) {
// this.studentDao = studentDao;
// }
@Override
public void add() {
// TODO Auto-generated method stub
studentDao.save();

}

}