spring框架4:bean的装配2(spring的核心功能)
2.基于注解的bean装配
一般会通过注解完成bean的配置,相对比较简单,减少工作量。
新建模块springBean2
说明:目录结构如上所示,实现的先后顺序如下:
1) pom.xml
2) User
3) UserDaoImp
4) UserService
5) UserController
6) applicationContext.xml
7) SpringTest
pom.xml
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.20</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> </dependency> </dependencies>
User
package com.scitc.domain; public class User { public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } private String id; private String name; @Override public String toString() { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + '}'; } }
UserDaoImp
package com.scitc.dao; import com.scitc.domain.User; import org.springframework.stereotype.Repository; @Repository public class UserDaoImp { public void addUser(User user){ System.out.println("3--dao层被调用"); System.out.println("4--完成数据库操作,添加一个用户,用户id:"+user.getId()+"姓名:"+user.getName()); } }
UserService
package com.scitc.service; import com.scitc.dao.UserDaoImp; import com.scitc.domain.User; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class UserService { @Resource public UserDaoImp userDao; public void addUser(User user){ userDao.addUser(user); } }
UserController
package com.scitc.controller; import com.scitc.domain.User; import com.scitc.service.UserService; import org.springframework.stereotype.Controller; import javax.annotation.Resource; @Controller //说明(重点理解) //@Controller注释替代了xml配置 <beaen id="userController" class="com.scitc.controller.UserController"/> //@Resource释替代了xml配置 <property name="userService" ref="userService"/> public class UserController { @Resource public UserService userService; public void addUser(User user){ System.out.println("2--service层被调用"); userService.addUser(user); } }
applicationContext.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--使用context命名空间,配置spring对指定包com下所有类进行扫描,并进行注解解析--> <context:component-scan base-package="com"/> </beans>
说明:注意引入顶部的3行context相关的命名约束
SpringTest
package com.scitc; import com.scitc.controller.UserController; import com.scitc.domain.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { @Test public void addUser(){ User user = new User(); user.setId("01"); user.setName("张三"); ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserController userController = (UserController)context.getBean("userController"); //输出Setter属性注入值 System.out.println("1--controller层被调用"); userController.addUser(user); } }
运行