『壹』 java spring自定義註解注入屬性注入不進去
不知道你說的「普通的java類」是什麼。
在spring里要注入的話,相應的類都要納入spring的管理,對象由spring創建,對象從spring獲取。就像你上面的BaseDaoImpl和BaseServiceImpl。
如果UserCarManager也是由spring管理的話,那同樣適用@autowired就可以注入。
看你的提問,應該是指沒有納入spring管理的類如何注入spring管理的對象,這種情況可以用srping相關的靜態方法達到目的。
比如:
WebApplicationContext webAppContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
BaseDaoImpl baseDao = (BaseDaoImpl) BeanFactoryUtils.beanOfTypeIncludingAncestors(webAppContext, BaseDaoImpl.class);
『貳』 JAVA使用Spring進行依賴注入怎麼實現
例如: UserService類有個getUsers()方法,我要在UserAction類使用這個方法這樣操作,
class UserAction{
private UserService userService;
//添加一個set方法
public void execute(){
List list = userService.getUsers();
}
}
//在spring配置文件進行依賴注入(如:UserService類在test包下的service包下)
<bean id="userService" class="test.service.UserService"/> //這就是依賴注入了
『叄』 Spring是如何實現注入的
在使用Spring的IOC功能的時候,Spring提供了集中注入方式:屬性注入,構造函數注入和工廠方法注入,更多的時候是使用的屬性注入,即set方法注入。使用set方法注入要求在寫bean的配置文件的時候,需要我們手動設置properties。諸如:
[java] view plain
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="testBean" class="com.jack.TestBean" scope="prototype"/>
<bean id="testAction" class="com.jack.TestAction" scope="prototype">
<property name="testBean" ref="testBean"/>
</bean>
</beans>
『肆』 JAVA的spring依賴注入的問題!!
<beans>
<beanid="zhanshenArmet">
<propertyname="name"value="戰神頭盔"/>
<propertyname="type"value="armet"/>
<propertyname="speedPlus"value="2"/>
<propertyname="attackPlus"value="4"/>
<propertyname="defencePlus"value="6"/>
</bean>
<beanid="suoziLoricae">
<propertyname="name"value="鎖子甲"/>
<propertyname="type"value="loricae"/>
<propertyname="speedPlus"value="2"/>
<propertyname="attackPlus"value="4"/>
<propertyname="defencePlus"value="6"/>
</bean>
<beanid="zhuifengBoot">
<propertyname="name"value="追風鞋"/>
<propertyname="type"value="boot"/>
<propertyname="speedPlus"value="8"/>
<propertyname="attackPlus"value="2"/>
<propertyname="defencePlus"value="3"/>
</bean>
<beanid="lanmoRing">
<propertyname="name"value="藍魔指環"/>
<propertyname="type"value="ring"/>
<propertyname="speedPlus"value="8"/>
<propertyname="attackPlus"value="12"/>
<propertyname="defencePlus"value="2"/>
</bean>
<beanid="player1">
<propertyname="armet"ref="zhanshenArmet"/>
<propertyname="loricae"ref="suoziLoricae"/>
<propertyname="boot"ref="zhuifengBoot"/>
<propertyname="ring"ref="lanmoRing"/>
</bean>
<beans>
『伍』 完全沒基礎要怎麼快速實現java spring注入和頁面跳轉需要先學什麼再學什麼
按次序來說 時間以一天10小時,全神關注來說
Java基本語法(一般 1~2周). 其中 Java反射重點掌握(spring注入是使用它的).
html基本語法(易 2天左右)
javascript for html 基本語法(一般 4天左右 就spring注入和頁面跳轉來說,不是必要的).
Java For Web,就是常說的JavaEE或者Servlet(難 1周).
學完以上,你在網上下一個spring的demo.就完全可以看懂和理解了.
如果要快速..直接在網上下載spring的demo,找懂的人幫你搭好環境,一天就可以跑起來,再跟你說清楚裡面的過程...不過你改不動而已.
『陸』 spring註解方式在一個普通的java類裡面注入
不知道你說的「普通的java類」是什麼。
在spring里要注入的話,相應的類都要納入spring的管理,對象由spring創建,對象從spring獲取。就像你上面的BaseDaoImpl和BaseServiceImpl。
如果UserCarManager也是由spring管理的話,那同樣適用@autowired就可以注入。
看你的提問,應該是指沒有納入spring管理的類如何注入spring管理的對象,這種情況可以用srping相關的靜態方法達到目的。
比如:
WebApplicationContext webAppContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
BaseDaoImpl baseDao = (BaseDaoImpl) BeanFactoryUtils.beanOfTypeIncludingAncestors(webAppContext, BaseDaoImpl.class);
『柒』 如何在Spring中注入Java集合類
//普通javabean
publicclassNotificationBean{
publicMaphobby;
publicMapgetHobby(){
returnhobby;
}
publicvoidsetHobby(Maphobby){
this.hobby=hobby;
}
}
<!--配置文件application-spring.xml-->
<beanid="notificationBean"class="com.straw.spring.beans.NotificationBean">
<propertyname="hobby">
<map>
<entrykey="a"value="a"/>
<entrykey="b"value="b"/>
</map>
</property>
</bean>
//java測試類
publicclassMainTest{
publicstaticvoidmain(String[]args){
=("application-spring.xml");
=(NotificationBean)ap.getBean("notificationBean");
Maphobby=notificationBean.getHobby();
Set<String>set=hobby.keySet();
for(Strings:set){
System.out.println(s+":"+hobby.get(s));
}
}
}
//運行結果
a:a
b:b
『捌』 java註解如何實現 spring 的數組的定義和注入
@Resource(name="myarray")
String[] arrayData;
在配置文件里這樣定義
<util:list id="myarray">
<value>hhhh</value>
<value>kkkk</value>
<value>oooo</value>
</util:list>
注意引入namingspace
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd」
『玖』 java,spring注入問題
new啥 不是用了注入了么
直接將【一個繼承ServiceImpl的類】配置給Service的ss就可以了