10.@Component注解的使用
注解的作用:取代了配置xml的配置bean繁琐。



IUserService .java
package com.gyf.service; import com.gyf.model.User; public interface IUserService { public void add(); public void add(User user); }
UserServiceImpl.java
package com.gyf.service; import com.gyf.dao.IUserDao; import com.gyf.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @Service("myUserService") public class UserServiceImpl implements IUserService { @Autowired //spring会自动注入userDao赋值 private IUserDao userDao; @Override public void add(User user) { System.out.println("service 添加用户:" + user); //调用dao userDao.add(user); } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void add() { System.out.println("创建用户...." + name); } public UserServiceImpl() { System.out.println("UserServiceImpl()调用了"); } }
UserAction.java
package com.gyf.web.action; import com.gyf.model.User; import com.gyf.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; @Controller public class UserAction{ @Autowired//spring自动注入userService赋值 @Qualifier("myUserService")//根据指定的id注入属性 private IUserService userService; public void save(User user){ System.out.println("action save方法 "); userService.add(user); } }
Lesson12.java
package com.gyf.test; import com.gyf.model.User; import com.gyf.service.IUserService; import com.gyf.web.action.UserAction; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Lesson12 { @Test public void test1() throws Exception { //注解的使用 //web开发流程 action -> service -> dao ApplicationContext context = new ClassPathXmlApplicationContext("beans12.xml"); //获取action UserAction userAction = context.getBean(UserAction.class); //添加用户 User user = new User(); user.setUsername("gyf"); user.setPassword("1234"); userAction.save(user); } }
Lesson10.java
package com.gyf.test; import com.gyf.model.Programmer; import com.gyf.model.User; import com.gyf.service.IUserService; import com.gyf.service.UserServiceImpl; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Lesson10 { @Test public void test1() throws Exception { //注解来取代xml中bean的配置 /** * 1.Spring默认情况下注解不生效,开启注解功能 * 2.如何开启spring注解功能? * 在.xml中配置下面两行代码 */ ApplicationContext context = new ClassPathXmlApplicationContext("beans10.xml"); /* 1.如果@Component没配置id,通过类型获取 IUserService service = (IUserService) context.getBean(IUserServiceImpl.class); 2.这个类型可以是接口,还可以是实现类 */ IUserService service = (IUserService) context.getBean(IUserService.class); //如果@Component("userService),配置了id,就可以通过id来获取 //IUserService service = (IUserService) context.getBean("userService"); User user = new User(); user.setUsername("gyf"); service.add(user); } }
bean10.xml
<?xml version="1.0" encoding="UTF-8"?> <!--xmlns xml namespace:xml命名空间--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p ="http://www.springframework.org/schema/p" 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"> <!--1.在类中声明@Component 相当于配置了 <bean class="com.gyf.service.UserServiceImpl"></bean> --> <!--<bean id="userService" class="com.gyf.service.UserServiceImpl"></bean>--> <!-- 开启注解--> <context:annotation-config/> <!-- 注解的位置--> <context:component-scan base-package="com.gyf"/> </beans>


浙公网安备 33010602011771号