发表于: 2018-01-14 22:47:50

1 621


一。不知道点击到什么地方了,导致idea中代码没有提示也不报错。

在百度搜了一下,发现在idea菜单栏中的File下倒数第二个Power Save Mode点击就可以了。

果然问题解决了。

要不然我还以为自己的代码完美无缺呢。


二。 继续Spring

<bean name="c" class="com.how2java.pojo.Category">
        <property
name="name" value="yyy" />
    </bean>
    <bean
name="p" class="com.how2java.pojo.Product">
        <property
name="name" value="product1" />
        <property
name="category" ref="c" />
    </bean>
    <bean
name="s" class="com.how2java.service.ProductService">
    </bean>
    <bean
id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>
    <
aop:config>
        <
aop:pointcut id="loggerCutpoint"
                     
expression="execution(* com.how2java.service.ProductService.*(..)) "/>
        <
aop:aspect id="logAspect" ref="loggerAspect">
                <
aop:around pointcut-ref="loggerCutpoint" method="log"/>

        </
aop:aspect>
</
aop:config>

applicationContext.XML中,庆东师兄告诉我只用从,cps等字母是不规范的。在程序已经跑通的情况下,我决定用category代替c,试一试能不能跑通。

运行成功,见图。:

 

三。下面试试注解方式AOP

AOP Aspect Oriented Program 面向切面编程 。首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ,所谓的周边功能,比如性能统计,日志,事务管理等等 。周边功能在Spring的面向切面编程AOP思想里,即被定义为切面 。在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发 。然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP。

 主要是配置ApplicationContext.xml文件。AOP的注解方式为:

  

<!--扫描包com.how2java.aspectcom.how2java.service,定位业务类和切面类

<context:component-scan base-package="com.how2java.aspect"/>
    <
context:component-scan base-package="com.how2java.service"/>
<!--找到被注解了的切面类,进行切面配置
    <
aop:aspectj-autoproxy/>

</beans>

 

四。注解配置切面

package com.how2java.aspect;

import
org.aspectj.lang.ProceedingJoinPoint;
import
org.aspectj.lang.annotation.Around;
import
org.aspectj.lang.annotation.Aspect;
import
org.springframework.stereotype.Component;

@Aspect //注解表示这是一个切面
@Component //表示这是一个bean,Spring进行管理
public class LoggerAspect {

   
@Around(value = "execution(* com.how2java.service.ProductService.*(..))")//表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作
   
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.
out.println("start log:" + joinPoint.getSignature().getName());
       
Object object = joinPoint.proceed();
       
System.out.println("end log:" + joinPoint.getSignature().getName());
        return
object;
   
}
}

 

五:

测试类注解。

package com.how2java.test;

import
com.how2java.pojo.Category;
import
org.junit.Test;
import
org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)//表示这是一个Spring的测试类
@ContextConfiguration("classpath:applicationContext.xml")//定位Spring的配置文件

public class TestSpring {
   
@Autowired //给这个测试类装配Category对象
   
Category category;

   
@Test //测试逻辑,打印c对象的名称
   
public void test(){
        System.
out.println(category.getName());

   
}
}

 

然后运行,出现错误。

严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@45b4c3a9] to prepare test instance [com.how2java.test.TestSpring@399c4be1]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.how2java.test.TestSpring': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.how2java.pojo.Category com.how2java.test.TestSpring.category; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.how2java.pojo.Category] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

百度之后,依然没有答案,有人出过类似的错误,是因为检查出来原因是多写了注解@Autowired 这个。但我检查之后,不是这个原因。

 

到最后,把ApplicationConteXT.xml里面扫描包和被注解了的切面类


<context:component-scan base-package="com.how2java.aspect"/>
<
context:component-scan base-package="com.how2java.service"/>

<
aop:aspectj-autoproxy/>

去掉了,然后加上了这么一段代码,运行,成功了。

<bean name="category" class="com.how2java.pojo.Category">
    <property
name="name" value="category 1" />
</bean>

 

运行成功图:


六。准备小课堂的PPT

 大概准备了百分之八十吧。


明天的计划:做完PPT,预讲一下小课堂。

遇到的问题:暂无

今天的收获:对applicationContext配置加深了了解,还是各种Spring注解。想来以后的mybatis注解也会更好理解

java任务一开始时间:2017.12.05

预计demo时间:2018.01-05

可能有延期风险,原因是:已经延期了,基础比较差,

禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html



返回列表 返回列表
评论

    分享到