我浏览了大约30个网页和50篇关于此的文章,它仍然无法正常工作。
这是我的代码,它非常简单,我只是一个Spring初学者。
App.java
@"movie"
SampleSimpleApplication.java
package com.procus.spring.simple.simple;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @author procus
*/
public class App {
public static void main(String[] args) {
ApplicationContext con = new ClassPathXmlApplicationContext("SpringBeans.xml");
SampleSimpleApplication sam = (SampleSimpleApplication) con.getBean("sam");
sam.run(args);
}
}
HelloWorldService.java
package com.procus.spring.simple.simple;
import com.procus.calculator.basic.BasicCalculator;
import com.procus.spring.simple.simple.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = BasicCalculator.class)
public class SampleSimpleApplication implements CommandLineRunner {
// intentional error
@Autowired
BasicCalculator calculator;
// Simple example shows how a command line spring application can execute an
// injected bean service. Also demonstrates how you can use @Value to inject
// command line args ('--name=whatever') or application properties
@Autowired
private HelloWorldService helloWorldService;
public SampleSimpleApplication() {
}
@Override
public void run(String... args) {
System.out.println(this.helloWorldService.getHelloMessage());
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleSimpleApplication.class, args);
}
}
SpringBeans.xml
package com.procus.spring.simple.simple.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Component;
@Component
@Configuration
@PropertySource(value = { "classpath:application.properties" })
public class HelloWorldService {
@Value("${app.name:World}")
private String name;
public String getHelloMessage() {
return "Hello " + this.name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
在resources文件夹中是我的application.properties文件,其中只包含 app.name = Phil
线程“main”中的异常 org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 来自类路径资源[SpringBeans.xml]的XML文档中的第12行是 无效;嵌套异常是org.xml.sax.SAXParseException; lineNumber:12; columnNumber:81; cvc-complex-type.2.4.c:匹配 通配符是严格的,但是找不到元素的声明 '上下文:属性占位符'。
我真的尝试过在stackoverflow和其他几个论坛上找到的大多数解决方案。我春天真的很新,我会感激任何帮助。
答案 0 :(得分:1)
我无法发表评论 -
我所看到的是你在写:<context:property-placeholder location="classpath*:application.properties"/>
为什么在类路径之后有*。
它应该与 - <context:property-placeholder location="classpath:application.properties"/>
其他 -
您可以使用 -
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
在你的Heloworld bean中
<bean id="helloworld" class ="package.HelloWorldService" >
<property name="name" value="${app.name}" />
</bean>
答案 1 :(得分:1)
在您的springbeans.xml
中,您已将位置指定为"classpath*:application.properties"
类路径,并在HelloWorldService.java
"classpath:application.properties"
中将位置指定为http://www.springframework.org/schema/context/spring-context-4.0.xsd
。这两个地方存在差异。
除了问题在于SpringBeans.xml模式声明。这是不正确的&amp;不完整的。
在缺少上下文声明<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
之后
检查this this&amp; this
理想情况下应该是
C11