Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6de875b

Browse files
Spring Configuración XML
Configurar el contenedor Spring con un archivo XML.
1 parent a89b07e commit 6de875b

File tree

8 files changed

+275
-0
lines changed

8 files changed

+275
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
10+
<config-files>
11+
<config-file>src/main/resources/beans.xml</config-file>
12+
</config-files>
13+
<config-file-groups/>
14+
</spring-data>
15+
</project-shared-configuration>

‎tutorial_config_xml/nbactions.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.args>-classpath %classpath carmelo.spring.xml.XMLConfig</exec.args>
14+
<exec.executable>java</exec.executable>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>process-classes</goal>
24+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
25+
</goals>
26+
<properties>
27+
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath carmelo.spring.xml.XMLConfig</exec.args>
28+
<exec.executable>java</exec.executable>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>-classpath %classpath carmelo.spring.xml.XMLConfig</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>

‎tutorial_config_xml/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>carmelo.spring.xml</groupId>
5+
<artifactId>tutorial_config_xml</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.springframework</groupId>
12+
<artifactId>spring-context</artifactId>
13+
<version>4.3.6.RELEASE</version>
14+
</dependency>
15+
</dependencies>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
<maven.compiler.target>1.8</maven.compiler.target>
21+
</properties>
22+
23+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package carmelo.spring.xml;
2+
3+
import java.util.List;
4+
import java.util.Set;
5+
6+
public class ListBean {
7+
8+
private List<String> items;
9+
10+
public List<String> getItems() {
11+
return items;
12+
}
13+
14+
public void setItems(List<String> items) {
15+
this.items = items;
16+
}
17+
18+
private Set<MyBean> beanItems;
19+
20+
public void setBeanItems(Set<MyBean> beanItems) {
21+
this.beanItems = beanItems;
22+
}
23+
24+
public Set<MyBean> getBeanItems() {
25+
return beanItems;
26+
}
27+
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package carmelo.spring.xml;
2+
3+
public class MyBean {
4+
5+
public MyBean() {
6+
}
7+
8+
public MyBean(Integer valor, String mensaje) {
9+
this.valor = valor;
10+
this.mensaje = mensaje;
11+
}
12+
13+
private Integer valor;
14+
private String mensaje;
15+
16+
public Integer getValor() {
17+
return valor;
18+
}
19+
20+
public void setValor(Integer valor) {
21+
this.valor = valor;
22+
}
23+
24+
public String getMensaje() {
25+
return mensaje;
26+
}
27+
28+
public void setMensaje(String mensaje) {
29+
this.mensaje = mensaje;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "MyBean{" + "valor=" + valor + ", mensaje=" + mensaje + '}';
35+
}
36+
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package carmelo.spring.xml;
2+
3+
public class OtherBean {
4+
5+
private MyBean myBean;
6+
7+
public OtherBean() {
8+
}
9+
10+
public OtherBean(MyBean myBean) {
11+
this.myBean = myBean;
12+
}
13+
14+
public MyBean getMyBean() {
15+
return myBean;
16+
}
17+
18+
public void setMyBean(MyBean myBean) {
19+
this.myBean = myBean;
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package carmelo.spring.xml;
2+
3+
import org.springframework.context.support.AbstractApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
public class XMLConfig {
7+
8+
public static void main(String[] args) {
9+
try(AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml")){
10+
11+
//MyBean bean = ctx.getBean("mybean", MyBean.class);
12+
13+
OtherBean other = ctx.getBean("otherbean", OtherBean.class);
14+
MyBean bean = other.getMyBean();
15+
16+
System.out.println("--> " + bean.getValor() + " - " + bean.getMensaje() + " <--");
17+
18+
ListBean list = ctx.getBean("listbean", ListBean.class);
19+
list.getBeanItems().forEach(System.out::println);
20+
}
21+
}
22+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:p="http://www.springframework.org/schema/p"
6+
xmlns:c="http://www.springframework.org/schema/c"
7+
xsi:schemaLocation="http://www.springframework.org/schema/beans
8+
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
9+
http://www.springframework.org/schema/context
10+
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
11+
12+
<!-- inyeccion por setter usando el namespace xmlns:p -->
13+
<bean id="mybean"
14+
class="carmelo.spring.xml.MyBean"
15+
p:valor="25" p:mensaje="Este es un mensaje..." />
16+
17+
<!-- inyeccion por setter usando el atributo <property> -->
18+
<bean id="mybean_1" class="carmelo.spring.xml.MyBean">
19+
20+
<!-- establecer una cadena vacia -->
21+
<property name="mensaje" value=""></property>
22+
23+
<!-- establecer un valor nullo -->
24+
<property name="valor" >
25+
<null/>
26+
</property>
27+
28+
</bean>
29+
30+
<bean id="mybean_2"
31+
class="carmelo.spring.xml.MyBean"
32+
p:valor="35" p:mensaje="" />
33+
34+
<!-- referenciar a otro bean -->
35+
<bean id="otherbean" class="carmelo.spring.xml.OtherBean">
36+
<!-- inyectar por constructor -->
37+
<!-- <constructor-arg ref="mybean" /> -->
38+
39+
<!-- inyectar por setter -->
40+
<property name="myBean" ref="mybean"/>
41+
</bean>
42+
43+
<!-- inyeccion de coleciones java -->
44+
<bean id="listbean" class="carmelo.spring.xml.ListBean">
45+
46+
<!-- agregar elementos a un java.util.List -->
47+
<property name="items">
48+
<list>
49+
<value>primer item</value>
50+
<value>segundo item</value>
51+
<value>tercer item</value>
52+
</list>
53+
</property>
54+
55+
<!-- agregar elementos a un java.util.Set -->
56+
<!-- esta es una collecion de beans -->
57+
<property name="beanItems">
58+
<set>
59+
<ref bean="mybean_1"/>
60+
<ref bean="mybean_2"/>
61+
</set>
62+
</property>
63+
64+
</bean>
65+
66+
<!-- agregar bean internos -->
67+
<bean id="innerbean" class="carmelo.spring.xml.OtherBean">
68+
<property name="myBean">
69+
<bean class="carmelo.spring.xml.MyBean"
70+
p:valor="10101" p:mensaje="InnerBean..." />
71+
</property>
72+
</bean>
73+
74+
<!--
75+
<bean id="mybean" class="carmelo.spring.xml.MyBean">
76+
<constructor-arg value="125" type="java.lang.Integer" />
77+
<constructor-arg value="Un Mensaje" type="java.lang.String" />
78+
</bean>
79+
-->
80+
81+
</beans>
82+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /