Spring之Annotation的使用
package com.fxr.spring.action; import java.util.List; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.fxr.spring.model.User; import com.fxr.spring.service.UserService; @Controller("useraction")//MVC控制层一般使用@Controller @Scope("prototype")//设置为多例模式 public class UserAction { private User u; private int id; private UserService userservice; private List<String>names; public List<String> getNames() { return names; } public void setNames(List<String> names) { this.names = names; } public UserAction(){ } public User getU() { return u; } public void setU(User u) { this.u = u; } public int getId() { return id; } public void setId(int id) { this.id = id; } public UserService getUserservice() { return userservice; } @Resource public void setUserservice(UserService userservice) { this.userservice = userservice; } public void add(){ userservice.add(u); } public void delete(){ userservice.delete(id); } } ============================================= package com.fxr.spring.dao; import com.fxr.spring.model.User; public interface IUserDao { public void add(User u); public void delete(int id); } =============================================== package com.fxr.spring.dao; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import com.fxr.spring.model.User; //@Component("userdao")//公共的创建bean的Annotation @Repository("userdao")//@Repository一般用于DAO的注入 public class UserDao implements IUserDao{ @Override public void add(User u) { System.out.println("添加"+u); } @Override public void delete(int id) { System.out.println("删除"+id); } } ================================================ package com.fxr.spring.service; import com.fxr.spring.dao.UserDao; import com.fxr.spring.model.User; public interface IUserService { public void add(User u); public void delete(int id); } ================================================ package com.fxr.spring.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import com.fxr.spring.dao.UserDao; import com.fxr.spring.model.User; @Component("userservice") @Service("userservice")//业务层一般使用@Service public class UserService implements IUserService{ private UserDao userdao; public UserDao getUserdao() { return userdao; } @Resource public void setUserdao(UserDao userdao) { this.userdao = userdao; } @Override public void add(User u) { userdao.add(u); } @Override public void delete(int id) { userdao.delete(id); } } ================================================= <?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"> <!-- 打开Spring的Annotation支持 --> <context:annotation-config/> <!-- 设定Spring 去哪些包中找Annotation --> <context:component-scan base-package="com.fxr.spring"/> </beans> ================================================== package com.fxr.spring.test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.fxr.spring.action.UserAction; import com.fxr.spring.model.HelloWorld; import com.fxr.spring.model.User; public class Test { private static BeanFactory factory = new ClassPathXmlApplicationContext("bean.xml"); public static void main(String[] args) { // //创建一个类 // HelloWorld hello = (HelloWorld) factory.getBean("helloworld"); // hello.say(); //创建一个类 UserAction ua1 = (UserAction) factory.getBean("useraction"); User u = new User(1,"张三"); ua1.setU(u); System.out.println(u); ua1.add(); // UserAction ua2 = (UserAction) factory.getBean("useraction"); // ua2.add(); } }