如何动态地将集合注入spring bean。 我所知 我的作者类的例子
import java.util.List;
public class Author {
private String name;
private List<String> listOfBooks;
public Author(String name, List<String> listOfBooks) {
this.name = name;
this.listOfBooks = listOfBooks;
}
public Author() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getListOfBooks() {
return listOfBooks;
}
public void setListOfBooks(List<String> listOfBooks) {
this.listOfBooks = listOfBooks;
}
}
我的bean id是:
<bean id="authorID" class="com.test.Author">
</bean>
以及我用作Authors类的@AutoWired
bean的一些地方。
如果我想将书籍添加到变量“listOfBooks”。
我该如何添加它。是通过使用对Authors对象的引用和访问getListOfBooks()方法然后添加值。或者这种方法有什么最好的方法。
感谢。
答案 0 :(得分:1)
<property name="addressList">
<list>
<value>Book1</value>
<value>Book2</value>
<value>Book3</value>
<value>Book4</value>
</list>
</property>
在你的上下文文件中:
有关详细信息,请参阅此post
答案 1 :(得分:1)
更详细的解释:
url1 <- url2 <- ""
shinyServer(function(input, output, session) {
values <- reactiveValues(myurl = c(), parent_tab = "")
observe({
# make sure this is called on pageload (to look at the query string)
# and whenever any tab is successfully changed.
# If you want to stop running this code after the initial load was
# successful so that further manual tab changes don't run this,
# maybe just have some boolean flag for that.
input$someID
input$tab_sub_tabs
query <- parseQueryString(session$clientData$url_search)
url <- query$url
if (is.null(url)) {
url <- ""
}
# "depth" is how many levels the url in the query string is
depth <- function(x) length(unlist(strsplit(x,"/")))
# if we reached the end, done!
if (length(values$myurl) == depth(url)) {
return()
}
# base case - need to tell it what the first main nav name is
else if (length(values$myurl) == 0) {
values$parent_tab <- "someID"
}
# if we're waiting for a tab switch but the UI hasn't updated yet
else if (is.null(input[[values$parent_tab]])) {
return()
}
# same - waiting for a tab switch
else if (tail(values$myurl, 1) != input[[values$parent_tab]]) {
return()
}
# the UI is on the tab that we last switched to, and there are more
# tabs to switch inside the current tab
# make sure the tabs follow the naming scheme
else {
values$parent_tab <- paste0(tail(values$myurl, 1), "_tabs")
}
# figure out the id/value of the next tab
new_tab <- unlist(strsplit(url, "/"))[length(values$myurl)+1]
# easy peasy.
updateTabsetPanel(session, values$parent_tab, new_tab)
values$myurl <- c(values$myurl, new_tab)
})
})
这样我们就是为java.util.List传递bean引用
记住:要使用上面的bean定义,你需要定义你的setter方法,使它们也应该能够处理引用。
<property name="bookList">
<list>
<value>Java</value>
<value>C++</value>
</list>
</property>
你的Getter和Setter将是:
ApplicationContext context =
new ClassPathXmlApplicationContext("YourBeans.xml");
Author a=(Author)context.getBean("authorID");
a.getListOfBooks();
也相应改变吸气剂。
谢谢
答案 2 :(得分:0)
Spring Injecting Collection在这里,您可以了解注入所有类型的集合[list,set,map
]和array
,并将其值作为基元/参考