Q:什么是Nutz。
A:类似于SSH的一个框架。官网http://www.nutzam.com/
Q:Nutz怎么读。
A:读作 “纳特Z(Z发重音)”。Nutz 的 Nut 是因为霍金的《果壳中的宇宙》是 zozoh 最喜欢的一本书之一。 Z 是 zozoh 小时,动画片《佐罗》给他很深的印象,尤其是每次转场的中间动画都是 佐罗的剑在黑色的空中 唰唰唰 三下划出锋利的 Z 字,好像三道闪电,酷的要命。 同时 zozoh 本人姓张,所以他很高兴在 Nut 后面 唰唰唰 的来一个 Z。
Q:Nutz可以做什么
A:Dao --针对 JDBC 的薄封装,事务模板,无缓存
Ioc -- JSON 风格的配置文件,声明时切片支持
Mvc -- 注解风格的配置,内置多文件上传功能
Json -- 解析和渲染
Castors -- Java 对象类型转换
Lang -- 更简洁的 Java 函数以及更丰富的反射支持
Aop -- 轻便快速的切面编程支持
Plugin -- 轻便的插件机制
Resource -- 资源扫描
它所有的功能均不依赖第三方 jar 包
这就意味着:
如果一个 Web 应用,你在 WEB-INF/lib 下只 需要放置一个 nutz.jar 就够了
当然你要使用连接池,数据库驱动等功能,还需要自行添置 jar 包。
最近的两三个小项目,别人搭了Nutz框架开发,阶段整理下。
首先正如上面所说Nutz真的很轻。一个nutz.jar 搞定。当然为了整个系统我引入了另外两个jar。和一些程序自带的jar。
druid-1.0.7.jar 是 阿里巴巴推出的国产数据库连接池;
h2-1.3.178.jar 是 h2内存数据库
这个demo例子 是 简单的swt开发的。官网的例子是MVCweb项目。最后总结区别。
项目代码结构图。
贴代码:
package demo.biz; import org.nutz.dao.entity.annotation.Column; import org.nutz.dao.entity.annotation.Id; import org.nutz.dao.entity.annotation.Name; import org.nutz.dao.entity.annotation.Table; @Table("t_person") public class Person { @Id private int id; @Name private String name; @Column private String age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
package demo.dao; import org.nutz.dao.Dao; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.IocBean; import demo.biz.Person; @IocBean(name = "personDao") public class PersonDao { @Inject private Dao dao; public void newPerson(Person person){ dao.insert(person); } }
package demo.service; import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.IocBean; import demo.biz.Person; import demo.dao.PersonDao; @IocBean(name = "personService") public class PersonService { @Inject PersonDao personDao; public void newPerson(Person person){ personDao.newPerson(person); } }
package demo; import org.nutz.ioc.Ioc; import org.nutz.ioc.impl.NutIoc; import org.nutz.ioc.loader.combo.ComboIocLoader; import org.nutz.log.Log; import org.nutz.log.Logs; /** * As you see... * */ public class IocMaster { private static final Log log = Logs.get(); private static Ioc ioc = null; public static Ioc getInstance() { if (null != ioc) { return ioc; } try { ioc = new NutIoc((new ComboIocLoader( "*org.nutz.ioc.loader.json.JsonLoader", "beans.js", "*org.nutz.ioc.loader.annotation.AnnotationIocLoader","demo" ))); } catch (ClassNotFoundException e) { String msg = "create ioc failed..."; log.error(msg); log.error(e.getMessage()); throw new RuntimeException(e); } return ioc; } }
下面这个是主页面。我是通过SWT开发的简单页面。
package demo; import org.eclipse.swt.SWT; @IocBean public class Main { protected Shell shell; private Text txtName1; private Text txtAge1; private Text txtName2; private Text txtAge2; private Table table; static PersonService personService; static PersonDao personDao; /** * Launch the application. * @param args */ public static void main(String[] args) { Ioc ioc = IocMaster.getInstance(); personService = ioc.get(PersonService.class); //personDao = ioc.get(PersonDao.class); try { Main window = new Main(); window.open(); } catch (Exception e) { e.printStackTrace(); } } /** * Open the window. */ public void open() { Display display = Display.getDefault(); createContents(); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } /** * Create contents of the window. */ protected void createContents() { shell = new Shell(); shell.setSize(856, 624); shell.setText("SWT Application"); Group group = new Group(shell, SWT.NONE); group.setText("\u65B0\u589E"); group.setBounds(10, 0, 820, 121); Label label = new Label(group, SWT.NONE); label.setBounds(20, 51, 61, 17); label.setText("\u540D\u79F0"); txtName1 = new Text(group, SWT.BORDER); txtName1.setBounds(99, 45, 111, 23); Label label_1 = new Label(group, SWT.NONE); label_1.setText("\u5E74\u9F84"); label_1.setBounds(255, 51, 61, 17); txtAge1 = new Text(group, SWT.BORDER); txtAge1.setBounds(334, 45, 111, 23); Button btnNew = new Button(group, SWT.NONE); btnNew.addMouseListener(new MouseAdapter() { @Override public void mouseUp(MouseEvent e) { String name = txtName1.getText(); String age = txtAge1.getText(); Person person = new Person(); person.setAge(age); person.setName(name); personService.newPerson(person); } }); btnNew.setBounds(594, 45, 80, 27); btnNew.setText("\u65B0\u589E"); Group group_1 = new Group(shell, SWT.NONE); group_1.setText("\u67E5\u8BE2"); group_1.setBounds(10, 127, 820, 449); Label label_2 = new Label(group_1, SWT.NONE); label_2.setText("\u540D\u79F0"); label_2.setBounds(20, 30, 61, 17); txtName2 = new Text(group_1, SWT.BORDER); txtName2.setBounds(99, 24, 111, 23); Label label_3 = new Label(group_1, SWT.NONE); label_3.setText("\u5E74\u9F84"); label_3.setBounds(255, 30, 61, 17); txtAge2 = new Text(group_1, SWT.BORDER); txtAge2.setBounds(334, 24, 111, 23); Button btnQuery = new Button(group_1, SWT.NONE); btnQuery.setText("\u67E5\u8BE2"); btnQuery.setBounds(596, 20, 80, 27); table = new Table(group_1, SWT.BORDER | SWT.FULL_SELECTION); table.setBounds(10, 87, 800, 352); table.setHeaderVisible(true); table.setLinesVisible(true); TableColumn tableColumn = new TableColumn(table, SWT.NONE); tableColumn.setWidth(354); tableColumn.setText("\u540D\u79F0"); TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE); tableColumn_1.setWidth(334); tableColumn_1.setText("\u5E74\u9F84"); } }
效果图(很丑- - 就是为了跑通亲测可用):
下面这个是创建数据库的运行一次即可.
package demo; import org.nutz.dao.Dao; import org.nutz.ioc.Ioc; import demo.biz.Person; public class MainCreareTable { public static void main(String[] args) { Ioc ioc = IocMaster.getInstance(); Dao dao = ioc.get(Dao.class); dao.create(Person.class,true); } }
bean.js配置文件
var ioc = { config : { type : "org.nutz.ioc.impl.PropertiesProxy", fields : { paths : ["beans.properties"] } }, dataSource: { type: "com.alibaba.druid.pool.DruidDataSource", events: { depose: 'close' }, fields: { driverClassName: {java :"$config.get('db-driver')"}, url : {java :"$config.get('db-url')"}, username : {java :"$config.get('db-username')"}, password : {java :"$config.get('db-password')"}, initialSize: 1, minIdle: 1, maxActive: 2, maxWait: 60000, timeBetweenEvictionRunsMillis: 60000, minEvictableIdleTimeMillis: 300000, poolPreparedStatements: true, maxPoolPreparedStatementPerConnectionSize: 20, testWhileIdle:true } }, dao: { type: "org.nutz.dao.impl.NutDao", args: [ {refer: "dataSource"} ] } };
beans.properties配置文件
db-driver=org.h2.Driver db-url=jdbc:h2:tcp://localhost/D:/gss/db/dbs/dbf --需要手动启动h2数据库模式 #db-url=jdbc:h2:D:/gss/db/dbs/dbf --内嵌数据库随着项目启动自行启动 db-username=gss db-password=gss
代码解释:
项目使用了 h2内存数据库为了方便测试,以免小伙伴机器上没有这个或者那个数据库啥的。
功能主要是一个新增数据。
bean.js和beans.properties配置文件主要是配置数据库连接池使用
其他类文件不赘述 大家都懂的
错误精读:错了错了错了 错在哪儿 为什么错 怎么错了 nutz框架 启动的入口是哪儿他凭什么神奇的注释哪儿哪儿就能用
在写这个demo 的时候,我之前做的web的项目 ioc容器是自动启动的,而在这个demo中需要手动启动,而我选择的加载文件夹给我造成了误解。我认为我在获得实例的时候我的文件夹下被我注释 @Inject的类都是可以直接使用使用的.类似这种概念:
@Inject private PersonService personService; 在下面的方法中... .... public void demo(){ personService.setXXX(); //事实证明这处的personService是空的 }
其实不然。
在web项目中 你会在 web.xml里面配置一个filter来指向你的mainmodule类。而mainmodule的注解是这样的
import org.nutz.mvc.annotation.Encoding; import org.nutz.mvc.annotation.IocBy; import org.nutz.mvc.annotation.Modules; import org.nutz.mvc.annotation.SetupBy; import org.nutz.mvc.ioc.provider.ComboIocProvider; @Encoding(input = "UTF-8", output = "UTF-8") @IocBy(type = ComboIocProvider.class, args = { "*org.nutz.ioc.loader.json.JsonLoader", "nutz/", "*org.nutz.ioc.loader.annotation.AnnotationIocLoader", "com.sgi.cari" }) @Modules(scanPackage = true) @SetupBy(value = MainSetup.class) public class MainModule { }
然后随着web容器的启动,该类加载进了ioc里面。
但是在java的项目中没有容器的配置,需要自行启动(IocMaster.java)的这个类被实例之后,还是需要
personService= ioc.get(PersonService.class);
来自己添加类进ioc对象来使用。
个人留存