Skip to content

Commit

Permalink
add attribute inject primative date
Browse files Browse the repository at this point in the history
  • Loading branch information
ieeningnwq committed Jan 16, 2024
1 parent 25ab7a3 commit 85e4276
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
23 changes: 23 additions & 0 deletions xmlinjectioccontainer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
<artifactId>spring-context</artifactId>
<version>5.3.28</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -80,6 +87,22 @@
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<goals>
<goal>sources</goal>
<goal>resolve</goal>
</goals>
<configuration>
<classifier>javadoc</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
19 changes: 19 additions & 0 deletions xmlinjectioccontainer/src/main/java/com/ieening/NormalBean.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
package com.ieening;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import lombok.Data;

@Data
public class NormalBean {

private Integer attributeInteger;
private String attributeString;
private Date attributeDate;
private String[] attributeStringArray;
private Set<String> attributeStringSet;
private List<String> attributeStringList;
private Map<String, String> attributeMap;
private Properties attributeProperties;

public NormalBean() {

}
Expand Down
17 changes: 15 additions & 2 deletions xmlinjectioccontainer/src/main/resources/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- ! 如何获取 Bean XML 配置 -->
<!-- ! 1、如何获取 Bean XML 配置 -->
<bean class="com.ieening.NormalBean" id="testGetBeanNormalBeanId"
name="testGetBeanNormalBeanName" />
<alias name="testGetBeanNormalBeanName" alias="testGetBeanNormalBeanNameAlias" />
<!-- ! 使用 -->

<!-- ! 2、通过依赖注⼊,在 spring 创建对象的同时,为其属性赋值。依赖注⼊的⽅式有三种:
Set注⼊
构造函数注⼊
⾃动注⼊
-->
<!-- ? 2.1 创建对象时,Spring ⼯⼚会通过 Set ⽅法为对象的属性赋值 -->
<bean class="com.ieening.NormalBean" id="testAttributeInjectNormalBeanId">
<!-- * bean attributes -->
<!-- * 2.1.1 基本数据类型 + 字符串类型 + 时间类型(注意格式) -->
<property name="attributeInteger" value="1" />
<property name="attributeString" value="string attribute" />
<property name="attributeDate" value="2024/01/16 08:25:20" />
</bean>
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.ieening;

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAttributeInjectBean {
private ClassPathXmlApplicationContext classPathXmlApplicationContext;

@Before
public void setUp() {
classPathXmlApplicationContext = new ClassPathXmlApplicationContext(
"spring.xml");
}

@Test
public void testAttributeInject() {
NormalBean normalBean = classPathXmlApplicationContext.getBean("testAttributeInjectNormalBeanId",
NormalBean.class);
assertEquals(
"NormalBean(attributeInteger=1, attributeString=string attribute, attributeDate=Tue Jan 16 08:25:20 CST 2024, attributeStringArray=null, attributeStringSet=null, attributeStringList=null, attributeMap=null, attributeProperties=null)",
normalBean.toString());
}

@After
public void tearDown() {
if (classPathXmlApplicationContext != null) {
classPathXmlApplicationContext.close();
}
}

}

0 comments on commit 85e4276

Please sign in to comment.