SpringMVC/Hibernate项目实践二

SpringMVC/Hibernate项目实践一
SpringMVC/Hibernate项目实践二
SpringMVC/Hibernate项目实践三

源码地址

第二步,引入Spring并实现IoC特征

项目结构.png
  • 在web.xml 中添加跟spring相关的配置信息
web.xml


    
    
        contextConfigLocation
        
            classpath*:/applicationContext.xml
        
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
  • 在resource中创建applicationContext.xml
applicationContext.xml


    
    
        
        
        
    

  • 创建包service,在其中创建UserService.java
UserService.java
@Service
public class UserService {

    public String test(){
        return "hello";
    }
}
  • 调整UserController.java,体现Spring IoC
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value="hello")
    public String hello(){
        return userService.test();
    }

}

现在我们启动一下项目,就能体现出IoC的特征了。

你可能感兴趣的:(SpringMVC/Hibernate项目实践二)