Spring搭建练习遇到的坑

时间:2024-07-20 16:35:56

1.Error:(7, 23) java: cannot find symbol
symbol: class ProceedingJoinPoint
location: class com.how2java.aspect.LoggerAspectSpring搭建练习遇到的坑

解决方法:

使用idea生成Spring框架.默认的jar里面没有这两个jar包,需要去maven仓库: https://mvnrepository.com/artifact/org.aspectj 找到下载这两个包

放入项目lib下,怎么添加参考: https://blog.****.net/chensanwa/article/details/79165528

Spring搭建练习遇到的坑

2.

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 30 in XML document from class path resource [spring-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 17; cvc-complex-type.2.4.c: The matching wildcard is strict,

Caused by: org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 17; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:config'.
错误原因是:

在spring-configuration.xml配置<aop : config> 按ALT+ENTER 会自动导入需要的xmlns,默认导入的是:

xmlns:aop="http://www.springframework.org/schema/p"
引发错误
解决办法:参考 https://*.com/questions/10062186/spring-aop-no-declaration-can-be-found-for-element-aopconfig
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" 3.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
Error creating bean with name 'com.how2java.test.TestSping1': Unsatisfied dependency expressed through field 'c'; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.how2java.pojo.Category' available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
出错原因:
未在spring-config.xml 中配置
<context:component-scan base-package="com.how2java.pojo"/>
而在测试类里面:Category在pake :com.how2java.pojo 包下面
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
public class TestSping1 {
@Autowired
Category c; @Test
public void test(){
System.out.println(c.getName());
}
}
解决方法:
在spring-config.xml 中配置
<context:component-scan base-package="com.how2java.pojo"/>