8.4.Spring注解练习

来源:互联网 发布:2017音乐软件市场份额 编辑:程序博客网 时间:2024/06/05 14:51

练习的注解有:@Controller 、@Service、@Repository  、自动配置注解@Autowired 、@Resource(两者的作用相差不大,优先使用@Resource)

1.目录


2.代码

StudentController.java

StudentDao.java(接口类)

StudentDaoImpl.java(接口实现类)

StudentService.java(接口类)

StudentServiceImpl.java(接口实现类)

Person.java

Test.java

beans.java

1.StudentController.java

package com.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import com.service.StudentService;@Controllerpublic class StudentController {@Autowiredprivate StudentService stuService;public String add(){System.out.println("我是控制层");stuService.add("李四四");return "add";}public void setStuService(StudentService stuService) {this.stuService = stuService;}}

2.StudentDao.java(接口类)

package com.dao;/* * 持久化数据层 */public interface StudentDao {public boolean add(String name);}

3.StudentDaoImpl.java(接口实现类)

package com.dao.impl;import org.springframework.stereotype.Repository;import com.dao.StudentDao;@Repositorypublic class StudentDaoImpl implements StudentDao{@Overridepublic boolean add(String name) {System.out.println("我是持久化层添加数据:"+name);return false;}}

4.StudentService.java(接口类)

package com.service;/* * 业务逻辑层 */public interface StudentService { public boolean add(String name);}

5.StudentServiceImpl.java(接口实现类)

package com.service.impl;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.dao.StudentDao;import com.service.StudentService;@Servicepublic class StudentServiceImpl implements StudentService{@Resourceprivate StudentDao dao;@Overridepublic boolean add(String name) {System.out.println("我是业务逻辑层:"+name);dao.add("张三1");return false;}public void setDao(StudentDao dao) {this.dao = dao;}}

6.Person.java

package com;import org.springframework.stereotype.Component;@Component(value="person1")public class Person {public void test() {System.out.println("用户信息");}}

7.Test.java

package test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.Person;import com.controller.StudentController;import com.dao.impl.StudentDaoImpl;import com.service.impl.StudentServiceImpl;public class Test {public static void main(String[] args) {ConfigurableApplicationContext bf=new ClassPathXmlApplicationContext("beans.xml");//Person类Person pe=(Person)bf.getBean("person1");pe.test();//持久化数据层(dao)StudentDaoImpl sd=(StudentDaoImpl)bf.getBean("studentDaoImpl");sd.add("张三");//业务逻辑层(service)StudentServiceImpl ss=(StudentServiceImpl)bf.getBean("studentServiceImpl");ss.add("李四");//控制层(controller)StudentController sc=(StudentController)bf.getBean("studentController");sc.add();}}

8.beans.java

<?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-3.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd"         >   <context:component-scan base-package="com">   <!-- 去除controller包下的StudentController类,既去除注解   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>    -->   </context:component-scan></beans>

结果:运行Test.java 控制台打印