发表于: 2018-03-16 23:55:56
2 612
一。今天解决了一个bug
代码是这样的:
public class Loopy { public static void main(String[] args){ int x = 1; System.out.println("Before the Loop"); while (x < 4) { System.out.println("In the loop"); System.out.println("Value of x is " + x); x = x +1 ; } System.out.println("This is after the loop"); } }
运行报错:
Error:(3, 8) java: 类Loopy是公共的, 应在名为 Loopy.java 的文件中声明
不知道为什么,删除public就可以了。
二。新建了个项目,学习Spring里面的注解,比如@Autowired注解
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Bean.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor.");
}
public void checkSpelling(){
System.out.println("Inside checkSpelling.");
}
}
public class TextEditor {
private SpellChecker spellChecker;
@Autowired
public void setSpellChecker(SpellChecker spellChecker){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}
Bean里面的配置是:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for textEditor bean without constructor-arg -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
一开始出BUG,说是读取不到Bean,后来发现Bean.xml写成Beans.xml了。
运行成功,简单说一下运行流程。
运行main方法,读取Bean.xml。运行第二句话,调用textEditor。然后转到bean.xml里面的
<!-- Definition for textEditor bean without constructor-arg -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
</bean>
这么一句话。class就指向了TxetEditor类。运行第一句话private SpellChecker spellChecker,就会转到SpellChecker类里面,运行第一个方法,也就是输出 Inside SpellChecker constructor.这么一句话。
紧接着运行te.spellCheck();,跳转到TextEditor里面的黄色方法。
public void spellCheck(){
spellChecker.checkSpelling();
}
调用checkSpelling,跳转到SpellChecker类里面,调用第二个方法public void checkSpelling,输出Inside checkSpelling这句话。
三。做了一个数组的java类。
public class BeerSong {
public static void main(String[] args){
int beerNum = 99;
String word = "bottles";
while (beerNum>0){
if(beerNum == 1){
word = "bottle";
}
System.out.println(beerNum + " " +word + "of beer on the wall");
System.out.println(beerNum + " " + word + "of beer.");
System.out.println("Take one down.");
System.out.println("Pass it around.");
beerNum = beerNum - 1;
if (beerNum>0){
System.out.println(beerNum + " " +word + "of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
}
}
自己用while做了个数组类,从头到尾自己写的。
public class Number {
public static void main(String[] args){
int i =1;
while(i<100){
System.out.println(i);
i = i+1;
就是从头到尾把1-99输出来了。想尝试着做一个从1+到100的数组类,结果没成功。暂时放弃。
明天的计划:继续任务一,Spring+mybatis
遇到的问题:spring再深一点就不理解了,继续学吧
今天的收获:学会了不少Spring注解
java任务二开始时间:2018.01.25
预计demo时间:2018.02.12
可能有延期风险,原因是:json看不懂,控制器的逻辑看不懂,所以又回看了java语法
禅道链接地址:http://task.ptteng.com/zentao/project-task-501.html
评论