天天看點

Spring Mvc那點事---(8)Spring Mvc @Resource注解

                    @Resource和@Autowired作用一樣,都是做bena注解使用[email protected]有按名稱和按類型兩種注入方式,預設是按名稱進行注入。

               按名稱注入

<bean id="applePhone" class="com.testone.Controller.ApplePhone" />
        <bean id="xiaomiPhone" class="com.testone.Controller.XiaoMiPhone" />
           

         接口     

public interface IMobilePhone {

	public String PhoneBrand();
}
           
public class XiaoMiPhone implements IMobilePhone {

	public String PhoneBrand() {
		// TODO Auto-generated method stub
		return "我是小米手機";
	}

}
           
public class ApplePhone implements IMobilePhone {

	public String PhoneBrand() {
		// TODO Auto-generated method stub
		return "我是蘋果手機";
	}

}
           
@Controller
@RequestMapping("/Home")
public class HomeController {

	@Resource(name="applePhone")
    private IMobilePhone phone;
	
	@RequestMapping(value="index")
	public String Index()
	{
		String msg=phone.PhoneBrand();
		
		System.out.print(msg);
		return "index";
	}
}
           

繼續閱讀